一闪水、什么是UIPopoverController
是iPad開發(fā)中常見的一種控制器
跟其他控制器不一樣的是,它直接繼承自NSObject孕索,并非繼承自UIViewController
它只占用部分屏幕空間來呈現(xiàn)信息澎办,而且顯示在屏幕的最前面
二、使用步驟
1.要想顯示一個UIPopoverController煌茬,需要經(jīng)過下列步驟
設置內容控制器
由于UIPopoverController直接繼承自NSObject,不具備可視化的能力
因此UIPopoverController上面的內容必須由另外一個繼承自UIViewController的控制器來提供彻桃,這個控制器稱為“內容控制器”
2.設置內容的尺寸
顯示出來占據(jù)多少屏幕空間
3.設置顯示的位置
從哪個地方冒出來
三坛善、設置內容控制器有3種方法
在初始化UIPopoverController的時候傳入一個內容控制器
- (id)initWithContentViewController:(UIViewController *)viewController;
@property (nonatomic, retain) UIViewController *contentViewController;
- (void)setContentViewController:(UIViewController *)viewController animated:(BOOL)animated;
以上方法和屬性都是UIPopoverController的
四、設置內容的尺寸
設置內容的尺寸有2種方法
@property (nonatomic) CGSize popoverContentSize;
- (void)setPopoverContentSize:(CGSize)size animated:(BOOL)animated;
以上方法和屬性都是UIPopoverController的
五邻眷、設置顯示的位置
設置顯示的位置有2種方法
1.圍繞著一個UIBarButtonItem顯示(箭頭指定那個UIBarButtonItem)
- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated;
item :圍繞著哪個UIBarButtonItem顯示
arrowDirections :箭頭的方向
animated :是否通過動畫顯示出來
2.圍繞著某一塊特定區(qū)域顯示(箭頭指定那塊特定區(qū)域)
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated;
rect :指定箭頭所指區(qū)域的矩形框范圍(位置和尺寸)
view :rect參數(shù)是以view的左上角為坐標原點(0眠屎,0)
arrowDirections :箭頭的方向
animated :是否通過動畫顯示出來
3.rect和view參數(shù)
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];
六肆饶、常見報錯
1.在popover的使用過程中改衩,經(jīng)常會遇到這個錯誤
-[UIPopoverController dealloc] reached while popover is still visible.
錯誤的大體意思是:popover在仍舊可見的時候被銷毀了(調用了dealloc)
從錯誤可以得出的結論
當popover仍舊可見的時候,不準銷毀popover對象
在銷毀popover對象之前驯镊,一定先讓popover消失(不可見)
七葫督、通過內容控制器設置內容尺寸
內容控制器可以自行設置自己在popover中顯示的尺寸
在iOS 7之前
@property (nonatomic,readwrite) CGSize contentSizeForViewInPopover;
從iOS 7開始
@property (nonatomic) CGSize preferredContentSize;
以上屬性都是UIViewController的
八竭鞍、常用屬性
代理對象
@property (nonatomic, assign) id <UIPopoverControllerDelegate> delegate;
是否可見
@property (nonatomic, readonly, getter=isPopoverVisible) BOOL popoverVisible;
箭頭方向
@property (nonatomic, readonly) UIPopoverArrowDirection popoverArrowDirection;
關閉popover(讓popover消失)
- (void)dismissPopoverAnimated:(BOOL)animated;
九、防止點擊UIPopoverController區(qū)域外消失
默認情況下
只要UIPopoverController顯示在屏幕上橄镜,UIPopoverController背后的所有控件默認是不能跟用戶進行正常交互的
點擊UIPopoverController區(qū)域外的控件偎快,UIPopoverController默認會消失
要想點擊UIPopoverController區(qū)域外的控件時不讓UIPopoverController消失,解決辦法是設置passthroughViews屬性
@property (nonatomic, copy) NSArray *passthroughViews;
這個屬性是設置當UIPopoverController顯示出來時洽胶,哪些控件可以繼續(xù)跟用戶進行正常交互晒夹。這樣的話,點擊區(qū)域外的控件就不會讓UIPopoverController消失了
十妖异、如何iPhone中實現(xiàn)popover的效果
1.UIPopoverController這個類是只能用在iPad中的
2.要想在iPhone中實現(xiàn)popover效果惋戏,必須得自定義view,可以參考
http://code4app.com/ios/Popover-View-in-iPhone/4fa931bd06f6e78d0f000000
http://code4app.com/ios/Popup-Menu/512231ac6803fa9e08000000
十一他膳、代碼示例
#import "ViewController.h"
#import "MenuViewController.h"
#import "OneViewController.h"
#import "ColorViewController.h"
#import "TestViewController.h"
@interface ViewController () <UIPopoverControllerDelegate>
- (IBAction)menuClick:(UIBarButtonItem *)sender;
- (IBAction)controllerClick:(UIBarButtonItem *)sender;
- (IBAction)selectColor:(UIButton *)sender;
- (IBAction)iOS8:(UIButton *)sender;
/** 菜單的Popover */
@property (nonatomic, strong) UIPopoverController *menuPopover;
/** 控制器的Popover */
@property (nonatomic, strong) UIPopoverController *controllerPopover;
/** 選擇顏色的Popover */
@property (nonatomic, strong) UIPopoverController *colorPopover;
/** iOS的測試Popover */
@property (nonatomic, strong) TestViewController *testVc;
@property (weak, nonatomic) IBOutlet UIButton *testButton;
@end
@implementation ViewController
#pragma mark - 事件的點擊
- (IBAction)menuClick:(UIBarButtonItem *)sender {
// 0.創(chuàng)建內容控制器
MenuViewController *menuVc = [[MenuViewController alloc] init];
// 1.創(chuàng)建UIPopoverController
UIPopoverController *menuPopover = [[UIPopoverController alloc] initWithContentViewController:menuVc];
// 2.設置內容控制器
// [menuPopover setContentViewController:menuVc];
// 3.設置尺寸
// [menuPopover setPopoverContentSize:CGSizeMake(100, 44 * 3)];
// 4.設置顯示的位置
[menuPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
// [menuPopover dismissPopoverAnimated:YES];
[self.menuPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
- (IBAction)controllerClick:(UIBarButtonItem *)sender {
[self.controllerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
- (IBAction)selectColor:(UIButton *)sender {
[self.colorPopover presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
- (IBAction)iOS8:(UIButton *)sender {
/*
// 1.創(chuàng)建準備以Popover形式彈出的控制器
TestViewController *testVc = [[TestViewController alloc] init];
// 2.設置彈出樣式
testVc.modalPresentationStyle = UIModalPresentationPopover;
// 3.設置在什么位置彈出
testVc.popoverPresentationController.sourceView = sender;
testVc.popoverPresentationController.sourceRect = sender.bounds;
*/
self.testVc.popoverPresentationController.sourceView = sender;
self.testVc.popoverPresentationController.sourceRect = sender.bounds;
[self presentViewController:self.testVc animated:YES completion:nil];
}
#pragma mark - 懶加載代碼
- (UIPopoverController *)menuPopover
{
if (_menuPopover == nil) {
// 1.創(chuàng)建內容控制器
MenuViewController *menuVc = [[MenuViewController alloc] init];
// 2.創(chuàng)建UIPopoverController,并且指定內容控制器
self.menuPopover = [[UIPopoverController alloc] initWithContentViewController:menuVc];
self.menuPopover.delegate = self;
}
return _menuPopover;
}
- (UIPopoverController *)controllerPopover
{
if (_controllerPopover == nil) {
// 1.創(chuàng)建內容控制器
OneViewController *oneVc = [[OneViewController alloc] init];
UINavigationController *oneNav = [[UINavigationController alloc] initWithRootViewController:oneVc];
// 2.創(chuàng)建UIPopoverController,并且設置內容控制器
_controllerPopover = [[UIPopoverController alloc] initWithContentViewController:oneNav];
}
return _controllerPopover;
}
- (UIPopoverController *)colorPopover
{
if (_colorPopover == nil) {
// 1.創(chuàng)建內容控制器
ColorViewController *colorVc = [[ColorViewController alloc] init];
// 2.創(chuàng)建UIPopoverController,并且指定內容控制器
self.colorPopover = [[UIPopoverController alloc] initWithContentViewController:colorVc];
__weak UIPopoverController *colorPover = _colorPopover;
__weak UIView *weakView = self.view;
colorVc.selectColorBlock = ^(UIColor *color) {
weakView.backgroundColor = color;
[colorPover dismissPopoverAnimated:YES];
};
}
return _colorPopover;
}
- (TestViewController *)testVc
{
if (_testVc == nil) {
// 1.創(chuàng)建控制器
_testVc = [[TestViewController alloc] init];
// 2.設置樣式
self.testVc.modalPresentationStyle = UIModalPresentationPopover;
}
return _testVc;
}
@end