Eventually:
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, below is the text, this is the picture on the height of the proportion 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 a member variable. Otherwise the method Btnclick call is finished to destroy, also 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. Auto Scaling 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. Listen to click "Location Positioning", pop up a popover controller-(void) locationbtnondockclicked{//disable, only 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 a member variable. Otherwise the method Btnclick call is finished to destroy, also how to display pinch? _popoverctrl = [[Uipopovercontroller alloc] INITWITHCONTENTVIEWCONTROLLER:CITYVC]; Set the size of the display of this PopOver controller _popoverctrl.popovercontentsize= Cgsizemake (320, 480); Agent, monitor PopOver controller xx Event _popoverctrl.delegate = self; Because other methods are also shown, _popoverctrl, so extract the custom method [self Showpopoverctrl]; Because the screen is rotated, the popover point of the popup is wrong, so it is necessary to register the monitoring screen rotation notification//First remove the listener to ensure robustness [[Nsnotificationcenter Defaultcenter] Removeobserve R:self name:uideviceorientationdidchangenotification Object:nil]; Add another 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 listen to the screen rotation, also want to display _popoverctrl again, so extracted into a custom method-(void) showpopoverctrl{//show where? If the target view is self, rect uses bounds, Because Bounds's origin 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 be restored by clicking the status self.enabled = YES; Remove registered listener (notification) [[Nsnotificationcenter Defaultcenter] removeobserver:self name before vanishing, i.e. PopOver is destroyed: Uideviceorientationdidchangenotification Object:nil];} #pragma mark-When destroying, remove the current monitor on the screen of the controller to prevent the wild pointer-(void) dealloc{[[Nsnotificationcenter Defaultcenter] removeobserver:self];} #pragma mark-Overwrite the frame-(cgrect) in the button to adjust the picture and text imagerectforcontentrect: (cgrect) contentrect{cgfloat btnw = contentrect.si Ze.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); The text is below, the picture is above cgfloat texty = Contentrect.size.height-texth; Return CGRectMake (0, Texty, BTNW, texth);} @end