先看他的继承关系,UIPopoverController是直接继承自NSObject,它和UIViewController没有关系.那它是怎么实现弹出在所有View之上的,我猜测是利用了keywindow,把这个View加在keywindow里面,我做了个试验,一般我们会在AppDelegate的didFinishLaunchingWithOptions中来初始化我们的window,把应用的第一个viewcontroller加到window中去,并在最后调用window的makekeyandvisible方法。于是我尝试在window实例调用makekeyandvisible方法的之前弹出一个UIPopoverController,于是得到了下面的错误:
***Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason:’-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.’
所以我猜UIPopoverController就是把你提供的Viewcontroller包起来加一个arrow框和背景,加到keywindow中去。另外如果你在显示地时候传入的BarButtonItem为nil,也会报这个错误,但实际上和window无关。
使用注意:
一、.UIPopoverController该控制器的内容必须由一个控制器提供;提供方式有3个:
1、
– (id)initWithContentViewController:(UIViewController *)viewController
//很简单的初始化方法,把你要展示的Viewcontroller传给它。
2、
-(void)setContentViewController:(UIViewController*)viewController animated:(BOOL)animated
//可以在UIPopoverController还在显示的时候动态地更换ContentViewController。
3、
@property (nonatomic, retain) UIViewController *contentViewController
二、设置内容大小:/* This property allows direction manipulation of the content size of the popover. Changing the property directly is equivalent to animated=YES. The content size is limited to a minimum width of 320 and a maximum width of 600. */ @property (nonatomic) CGSize popoverContentSize; – (void)setPopoverContentSize:(CGSize)size animated:(BOOL)animated;
/* contentSizeForViewInPopover allows you to set the size of the content from within the view controller. This property is read/write, and you should generally not override it. */ @property (nonatomic,readwrite) CGSize contentSizeForViewInPopover NS_AVAILABLE_IOS(3_2);
三.设置箭头方向:@property (nonatomic, readonly) UIPopoverArrowDirection popoverArrowDirection
typedef NS_OPTIONS(NSUInteger, UIPopoverArrowDirection) {
UIPopoverArrowDirectionUp = 1UL << 0,
UIPopoverArrowDirectionDown = 1UL << 1,
UIPopoverArrowDirectionLeft = 1UL << 2,
UIPopoverArrowDirectionRight = 1UL << 3,
UIPopoverArrowDirectionAny = UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown | UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionRight,
UIPopoverArrowDirectionUnknown = NSUIntegerMax
};
四、
1、如果从一个导航按钮处呈现,使用:presentPopoverFromBarButtonItem:permittedArrowDirections:animated:;
这种方式弹出的popVC接近于模态,但不完全是模态,因为NavigationBar上的所有按钮都高于这个popVC层,也就是说NavigationBar上的按钮可以继续响应用户的操作,而不管当前是否有从BarButtonItem弹出的popVC。
这样就有可能引发一些问题,如,我们再次点击这个BarButtonItem时,则又会执行一次弹出操作,实际上界面上将会有两个popOver,更明显的问题是,如果我们点navigationBar上的返回按钮,把当前这个界面pop出去,则会因为当前还有展示的popVC而使当前界面崩溃。
一般实现:
– (IBAction)languageButtonTapped {
if (self.languagePopoverController == nil) {
BIDLanguageListController *languageListController =
[[BIDLanguageListController alloc] init];
languageListController.detailViewController = self;
UIPopoverController *poc = [[UIPopoverController alloc]
initWithContentViewController:languageListController];
[poc presentPopoverFromBarButtonItem:languageButton
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
self.languagePopoverController = poc;
} else {
if (languagePopoverController != nil) {
[languagePopoverController dismissPopoverAnimated:YES];
self.languagePopoverController = nil;
}
}
}
2、如果要从一个视图出呈现,使用:presentPopoverFromRect:inView:permittedArrowDirections:animated:
这个方法需要传入一个CGRect和一个View,而只有这个CGRect的值是相对于这个View的,这个Arrow才能指到正确的位置。我猜测它是在视图树中向上搜索把它转化为在keywindow中的值再显示。举个例子,如果你要箭头指向被点击的这个button,
那么一种方法是:
[xxx presentPopoverFromRect:button.boundsinView:buttonpermittedArrowDirections:xx animated:YES];
一种方法是转换为他的父视图中的CGRect:
[xxx presentPopoverFromRect:button.frameinView:button.superviewpermittedArrowDirections:xx animated:YES];
// inView的中心点是用来画箭头的,如果中心点如果出了屏幕,系统会优化到窗口边缘
//inView是个CGRectMake(x0, y0, x1, y1),如果你想绝对定位的话,可以把x1,y1设置为0,x0,y0就是箭头的位置。
这种方式弹出的popVC就是绝对的模态了,不把这个popVC消隐,其它任何地方,包括NavigtionBar都得到不交互。
注意:如果设备旋转以后,位置定位错误需要在父视图控制器的下面方法里面重新定位:didRotateFromInterfaceOrientation:(在这个方法体里面重新设置rect)然后再次调用:- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated
五、UIPopoverController的外观
所有欺骗中,自欺是最为严重的