1、概述ios
是iPad開發中常見的一種控制器(在iPhone上不容許使用),跟其餘控制器不同的是,它直接繼承自NSObject,並不是繼承自UIViewController,它只佔用部分屏幕空間來呈現信息,並且顯示在屏幕的最前面。app
2、使用步驟動畫
要想顯示一個UIPopoverController,須要通過下列步驟:atom
第一步:設置內容控制器代理
因爲UIPopoverController直接繼承自NSObject,不具有可視化的能力。所以UIPopoverController上面的內容必須由另一個繼承自UIViewController的控制器來提供,這個控制器稱爲「內容控制器」。code
設置內容控制器有3種方法:對象
(1)在初始化UIPopoverController的時候傳入一個內容控制器:繼承
- (id)initWithContentViewController:(UIViewController *)viewController;開發
(2)@property (nonatomic, retain) UIViewControllerget
*contentViewController;
(3)- (void)setContentViewController:
(UIViewController *)viewController animated:(BOOL)animated;
以上方法和屬性都是UIPopoverController的。
第二步:設置內容的尺寸
即設置顯示出來佔據多少屏幕空間。
設置內容的尺寸有2種方法:
(1)@property (nonatomic) CGSize popoverContentSize;
(2)- (void)setPopoverContentSize:(CGSize)size animated:
(BOOL)animated;
以上方法和屬性都是UIPopoverController的。
也能夠在UIPopoverController的ContentViewController(內容控制器)的viewDidLoad方法中設置popoverContentSize來設置其顯示在UIPopoverController中的大小。
第三步:設置顯示的位置
即設置從哪一個地方冒出來。
設置顯示的位置有2種方法:
(1)圍繞着一個UIBarButtonItem顯示(箭頭指定那個UIBarButtonItem)
- (void)PopoverFromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated;
item :圍繞着哪一個UIBarButtonItem顯示
arrowDirections :箭頭的方向
animated :是否經過動畫顯示出來
(2)圍繞着某一塊特定區域顯示(箭頭指定那塊特定區域)
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated;
rect :指定箭頭所指區域的矩形框範圍(位置和尺寸)
view :rect參數是以view的左上角爲座標原點(0,0)
arrowDirections :箭頭的方向
animated :是否經過動畫顯示出來
3、rect和view參數
4、設置顯示的位置
若是想讓箭頭指向某一個UIView的作法有2種作法,好比指向一個button。
方法1:
[popover presentPopoverFromRect:button.bounds inView:button permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
方法2:
[popover presentPopoverFromRect:button.frame
inView:button.superview permittedArrowDirections:
UIPopoverArrowDirectionDown animated:YES];
5、常見報錯
在popover的使用過程當中,常常會遇到這個錯誤:
-[UIPopoverController dealloc] reached while popover is still visible.
錯誤的大致意思是:popover在仍舊可見的時候被銷燬了(調用了dealloc)。
從錯誤能夠得出的結論:
(1)當popover仍舊可見的時候,不許銷燬popover對象
(2)在銷燬popover對象以前,必定先讓popover消失(不可見)
例如:
-(IBAction)menuClick:(UIBarButtonItem *)sender {
//建立內容控制器
UIViewController *vc = [[UIVeiwController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
UINavigationController *nav = [[UINavigationController alloc] initWithRootController:vc];
//建立popover
UIPopoverController *titlePopover = [[UIPopoverController alloc] initWithContentViewController:nav];
//能夠不設置尺寸,會有一個默認的尺寸320x480
//設置顯示到那個位置
[titlePopover presentPopoverFromBarButtonItem:sender permittedArrowDirectionAny animated:YES];
}
上面代碼就會報這個錯誤,由於在ARC模式下,當執行到最後一句時,titlePopover就會被系統回收,但此時titlePopover仍然顯示在屏幕上,因此會報上面錯誤。解決辦法是建立一個屬性:
@property (nonatomic, strong) UIPopoverController *titlePopover;
把上面代碼紅色部分所有改爲self.titlePopover
6、經過內容控制器設置內容尺寸
內容控制器能夠自行設置本身在popover中顯示的尺寸:
在iOS 7以前:
@property (nonatomic,readwrite) CGSize contentSizeForViewInPopover;
從iOS 7開始:
@property (nonatomic) CGSize preferredContentSize;
以上屬性都是UIViewController的
7、經常使用屬性
代理對象:
@property (nonatomic, assign) id <UIPopoverControllerDelegate> delegate;
是否可見:
@property (nonatomic, readonly, getter=isPopoverVisible) BOOL popoverVisible;
箭頭方向:
@property (nonatomic, readonly) UIPopoverArrowDirection popoverArrowDirection;
關閉popover(讓popover消失):
- (void)dismissPopoverAnimated:(BOOL)animated;
8、防止點擊UIPopoverController區域外消失
默認狀況下:
(1)只要UIPopoverController顯示在屏幕上,UIPopoverController背後的全部控件默認是不能跟用戶進行正常交互的
(2)點擊UIPopoverController區域外的控件,UIPopoverController默認會消失
要想點擊UIPopoverController區域外的控件時不讓UIPopoverController消失,解決辦法是設置passthroughViews屬性:
@property (nonatomic, copy) NSArray *passthroughViews;
這個屬性是設置當UIPopoverController顯示出來時,哪些控件能夠繼續跟用戶進行正常交互。這樣的話,點擊區域外的控件就不會讓UIPopoverController消失了
七、 如何iPhone中實現popover的效果
UIPopoverController這個類是隻能用在iPad中的,要想在iPhone中實現popover效果,必須得自定義view,能夠參考:
http://code4app.com/ios/Popover-View-in-iPhone/4fa931bd06f6e78d0f000000