什么是UIPopoverControllerios
- 是iPad开发中常见的一种控制器
- 跟其余控制器不同的是,它直接继承自NSObject,并不是继承自UIViewController
- 它只占用部分屏幕空间来呈现信息,并且显示在屏幕的最前面



使用步骤app
- 要想显示一个UIPopoverController,须要通过下列步骤
- 因为UIPopoverController直接继承自NSObject,不具有可视化的能力
- 所以UIPopoverController上面的内容必须由另一个继承自UIViewController的控制器来提供,这个控制器称为“内容控制器”
1.设置内容控制器动画
- 在初始化UIPopoverController的时候传入一个内容控制器
- (id)initWithContentViewController:(UIViewController *)viewController;atom
- @property (nonatomic, retain) UIViewController *contentViewController;
- - (void)setContentViewController:(UIViewController *)viewController animated:(BOOL)animated;
以上方法和属性都是UIPopoverController的spa
2.设置内容的尺寸代理
- @property (nonatomic) CGSize popoverContentSize;
- - (void)setPopoverContentSize:(CGSize)size animated:(BOOL)animated;
3.设置显示的位置code
- 围绕着一个UIBarButtonItem显示(箭头指定那个UIBarButtonItem)
- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated;对象
- item :围绕着哪一个UIBarButtonItem显示
- arrowDirections :箭头的方向
- animated :是否经过动画显示出来
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated;继承
- rect :指定箭头所指区域的矩形框范围(位置和尺寸)
- view :rect参数是以view的左上角为坐标原点(0,0)
- arrowDirections :箭头的方向
- animated :是否经过动画显示出来
常见报错开发
- 在popover的使用过程当中,常常会遇到这个错误
-[UIPopoverController dealloc] reached while popover is still visible.
- 错误的大致意思是:popover在仍旧可见的时候被销毁了(调用了dealloc)
- 当popover仍旧可见的时候,不许销毁popover对象
- 在销毁popover对象以前,必定先让popover消失(不可见)
经过内容控制器设置内容尺寸
- 内容控制器能够自行设置本身在popover中显示的尺寸
@property (nonatomic,readwrite) CGSize contentSizeForViewInPopover;
@property (nonatomic) CGSize preferredContentSize;
以上属性都是UIViewController的
经常使用属性
@property (nonatomic, assign) id <UIPopoverControllerDelegate> delegate;
@property (nonatomic, readonly, getter=isPopoverVisible) BOOL popoverVisible;
@property (nonatomic, readonly) UIPopoverArrowDirection popoverArrowDirection;
- (void)dismissPopoverAnimated:(BOOL)animated;
防止点击UIPopoverController区域外消失
- 只要UIPopoverController显示在屏幕上,UIPopoverController背后的全部控件默认是不能跟用户进行正常交互的
- 点击UIPopoverController区域外的控件,UIPopoverController默认会消失
- 要想点击UIPopoverController区域外的控件时不让UIPopoverController消失,解决办法是设置passthroughViews属性
@property (nonatomic, copy) NSArray *passthroughViews;
- 这个属性是设置当UIPopoverController显示出来时,哪些控件能够继续跟用户进行正常交互。这样的话,点击区域外的控件就不会让UIPopoverController消失了
如何iPhone中实现popover的效果
- UIPopoverController这个类是只能用在iPad中的
- 要想在iPhone中实现popover效果,必须得自定义view,能够参考