ios_21 buy _popover adapt to ipad screen switch

Source: Internet
Author: User

Finally:



Code snippet:

dockitemlocation.m//Handsome _ buy////Created by Beyond on 14-8-13.//Copyright (c) 2014 Com.beyond. All rights reserved.//#import "DockItemLocation.h"//click on the locationbtn above the dock to eject the PopOver encapsulated controller, above it is the search bar, below is tableview# Import "CityLocationController.h"//button above is the picture, the following is the text, this is the picture on the height of the ratio of # define KIMAGEHEIGHTRATIOINBTN 0.5@interface Dockitemlocation () <uipopovercontrollerdelegate>{//popover Controller, after it is created, the Show method is displayed, so it cannot be a local variable and must be remembered with member variables, Otherwise, the method Btnclick call completion is destroyed, how to display pinch?

Uipopovercontroller *_popoverctrl;} @end @implementation dockitemlocation-(ID) initWithFrame: (cgrect) frame{self = [super Initwithframe:frame]; if (self) {///1. Call the parent class method, set the internal picture [self seticon:@ "ic_district.png" selectedicon:@ "Ic_district_hl.png"]; 2. Self-active telescopic self.autoresizingmask = Uiviewautoresizingflexibletopmargin; 3. Set the default text [self settitle:@ "forstate:uicontrolstatenormal]; Self.titleLabel.font = [Uifont systemfontofsize:16]; Self.titleLabel.textAlignment = Nstextalignmentcenter; [Self Settitlecolor:[uicolor whitecolor] forstate:uicontrolstatedisabled]; [Self Settitlecolor:[uicolor graycolor] forstate:uicontrolstatenormal]; 4. Set picture Properties Self.imageView.contentMode = Uiviewcontentmodecenter; 5. Tap "Location Locator" to eject a popover controller [self addtarget:self Action: @selector (locationbtnondockclicked) Forcontroleven Ts:uicontroleventtouchdown]; } RETurn self;} 5. Monitor Click "Location Positioning", pop up a popover controller-(void) locationbtnondockclicked{//disable, just click once self.enabled = NO; Click on the locationbtn above the dock to eject the PopOver encapsulated controller, above it is the search bar, below is tableview citylocationcontroller *CITYVC = [[ Citylocationcontroller alloc] init]; The only controller that is not inherited from Uiviewcontroller, which inherits from the NSObject//popover controller, is created, the Show method is displayed, so it cannot be a local variable and must be remembered with member variables, Otherwise, the method Btnclick call completion is destroyed, how to display pinch? _popoverctrl = [[Uipopovercontroller alloc] INITWITHCONTENTVIEWCONTROLLER:CITYVC]; Set the display size of this popover controller _popoverctrl.popovercontentsize = Cgsizemake (320, 480); Agent, monitor PopOver controller xx Event _popoverctrl.delegate = self; As other methods are also shown, _popoverctrl, so extracted into their own definition method [self Showpopoverctrl]; As the screen rotates, the popover point of the popup is not correct, so it is necessary to register to monitor the screen rotation notification//First remove the listener to ensure robustness [[Nsnotificationcenter Defaultcenter] Removeobserv Er:self name:uideviceorientationdidchangenotification Object:nil]; Add a listener, and once the device appears uideviceorientationdidchangenotification, the Observer selector method is called [[nsnotificationceNter Defaultcenter] addobserver:self selector: @selector (screendidrotated) Name: Uideviceorientationdidchangenotification Object:nil];} 5-1, because the hearing screen rotation, but also to show _popoverctrl again, so extracted into their own definition method-(void) showpopoverctrl{//show where?

If the target view is self, then rect uses bounds, because the origin of bounds is relative to itself//if the target view is Self.superview, then rect uses frame, because the frame's origin is relative to the parent control [_ Popoverctrl presentPopoverFromRect:self.bounds inview:self Permittedarrowdirections:uipopoverarrowdirectionany Animated:yes];} 5-2, add a listener, once the device appears uideviceorientationdidchangenotification, it will call the Observer Selector Method-(void) screendidrotated{ if (_popoverctrl.popovervisible) {//1. Close the previous position above the _popoverctrl [_popoverctrl Dismisspopoveranimated:no]; 2. 0.5 seconds after creating a new location on the _popoverctrl [self performselector: @selector (Showpopoverctrl) Withobject:nil afterdelay:0.5]; }} #pragma mark-popover proxy Method-(void) Popovercontrollerdiddismisspopover: (Uipopovercontroller *) popovercontroller{// Before disappearing, let the position button recover to be able to click the state self.enabled = YES; Before disappearing, that is, when the PopOver is destroyed. Listener to remove the Register (notification) [[Nsnotificationcenter Defaultcenter] removeobserver:self name: Uideviceorientationdidchangenotification Object:nil];} #pragma mark-Removes the current monitor of the screen from the controller, preventing the Wild Pointer-(void) Dealloc{[[Nsnotificationcenter Defaultcenter] removeobserver:self];} #pragma mark-Overwrite the frame-(cgrect) in the button with the Adjust picture and text imagerectforcontentrect: (cgrect) contentrect{cgfloat btnw = Contentrec T.size.width; CGFloat IMGH = contentRect.size.height * KIMAGEHEIGHTRATIOINBTN; Return CGRectMake (0, 0, BTNW, IMGH);} -(CGRect) Titlerectforcontentrect: (cgrect) contentrect{cgfloat btnw = contentRect.size.width; CGFloat texth = contentRect.size.height * (1-KIMAGEHEIGHTRATIOINBTN); Text in the following, picture on top cgfloat texty = Contentrect.size.height-texth; Return CGRectMake (0, Texty, BTNW, texth);} @end








ios_21 buy _popover adapt to ipad screen switch

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.