效果:
1.對UIPopoverController的簡單概述
2.UIPopoverController實現
要想成功顯示一個UIPopoverController,需要經過下列步驟:
2.2設置內容控制器
由於UIPopoverController直接繼承自NSObject,不具備可視化的能力,因此UIPopoverController上面的內容必須由另外一個繼承自UIViewController的控制器來提供,這個稱為“內容控制器”
設置內容控制器有三種方法:
在初始化UIPopoverController的時候傳入一個內容控制器
通過@property設置內容控制器
animated可以指定設置內容控制器時要不要帶有動畫效果
1 @interfaceQCLocationButton() <UIPopoverControllerDelegate> 2 3 { 4 5 UIPopoverController *_popover; 6 7 }
// 2.彈出popover(默認特性:點擊popover之外的任何地方,popover都會隱藏) // 2.1.內容 QCCityListViewController *cityList = [[QCCityListViewController alloc] init]; // 2.2.將內容塞進popover中 _popover = [[UIPopoverController alloc] initWithContentViewController:cityList];
2.3設置內容的尺寸
顯示出來占據多少屏幕空間
設置內容的尺寸有兩種方法:
1 // 2.3.設置popover的內容尺寸 2 _popover.popoverContentSize = CGSizeMake(320, 480);
2.4設置顯示的位置
從哪個地方冒出來
設置顯示的位置有兩種方法:
[pop presentPopoverFromRect:button.bounds inView:button permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
[pop presentPopoverFromRect:button.frame inView:button.superview permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
箭頭會指向某一個UIBarButtonItem
假如iPad的屏幕發生了旋轉,UIPopoverController顯示的位置可能會改變,那么就需要重寫控制器的某個方法
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
在上面的方法中重寫設置UIPopoverController顯示的位置
1 // 2.5.展示popover 2 // self.bounds --- self 3 // self.frame --- self.superview 4 [_popover presentPopoverFromRect:self.bounds inView:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
清澈Saup