iOS开发之UIPopoverController

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 :是否经过动画显示出来

3rectview参数

 

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

http://code4app.com/ios/Popup-Menu/512231ac6803fa9e08000000

相关文章
相关标签/搜索