UIPopoverController是Pad設備中經常使用的一種視圖控制器,其在UI表現上爲在當前視圖控制器上面彈出一個子視圖控制器,一般用來展現交互列表。示例以下圖:數組
UIPopoverController只能用於iPad,在要兼容iPad和iPhone的項目中,須要根據設備類型使用兩套代碼。在iOS8以後,系統提供了UIPresentationController來代替她,UIPresentationController能夠兼容iPhone與iPad。動畫
首先UIPopoverController是一個容器控制器,其中須要承載一個ViewControler做爲內容視圖。UIPopoverController使用以下初始化方法建立:atom
//建立視圖控制器的方法 經過一個內容視圖控制器建立 - (instancetype)initWithContentViewController:(UIViewController *)viewController;
建立出控制器後,調用以下方法能夠將控制器彈出:spa
//這個方法將控制器以一個CGRect區域爲基準彈出 /* UIPopoverArrowDirection爲箭頭出現的方向 typedef NS_OPTIONS(NSUInteger, UIPopoverArrowDirection) { UIPopoverArrowDirectionUp = 1UL << 0,//上 UIPopoverArrowDirectionDown = 1UL << 1,//下 UIPopoverArrowDirectionLeft = 1UL << 2,//左 UIPopoverArrowDirectionRight = 1UL << 3,//右 UIPopoverArrowDirectionAny = UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown | UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionRight,//任意方向 UIPopoverArrowDirectionUnknown = NSUIntegerMax//未知 }; */ //view參數爲選擇要在那個View視圖上彈出 animated參數設置是否帶動畫 - (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated; //以一個BarButtonItem爲基準彈出 其他參數意義同上 - (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated;
UIPopoverController的相關設置方法以下:代理
//設置代理 @property (nullable, nonatomic, weak) id <UIPopoverControllerDelegate> delegate; //設置內容視圖控制器 @property (nonatomic, strong) UIViewController *contentViewController; - (void)setContentViewController:(UIViewController *)viewController animated:(BOOL)animated; //設置界面展現尺寸 @property (nonatomic) CGSize popoverContentSize; - (void)setPopoverContentSize:(CGSize)size animated:(BOOL)animated; //獲取控制器當前是否正在展現 @property (nonatomic, readonly, getter=isPopoverVisible) BOOL popoverVisible; //獲取控制器箭頭方向 @property (nonatomic, readonly) UIPopoverArrowDirection popoverArrowDirection; //這個屬性能夠加強控制器的交互能力 /* 默認狀況下,當視圖控制器彈出時,點擊界面上的其餘位置,視圖控制器會被隱藏 若是須要當視圖控制愛彈出時界面上的其餘控件依然能夠進行用戶交互,則須要將這些UI控件設置進這個數組中 */ @property (nullable, nonatomic, copy) NSArray<__kindof UIView *> *passthroughViews; //隱藏視圖控制器的方法 - (void)dismissPopoverAnimated:(BOOL)animated; //設置視圖控制器的背景顏色 @property (nullable, nonatomic, copy) UIColor *backgroundColor NS_AVAILABLE_IOS(7_0); //設置視圖Margin @property (nonatomic, readwrite) UIEdgeInsets popoverLayoutMargins NS_AVAILABLE_IOS(5_0); //這個屬性用於自定義PopoverController的UI展示 傳入自定義的背景視圖類 @property (nullable, nonatomic, readwrite, strong) Class popoverBackgroundViewClass NS_AVAILABLE_IOS(5_0);
經過設置UIPopoverController對象的popoverBacjgroundViewClass屬性能夠將一個自定義的類做爲控制器的背景視圖,須要注意,此自定義的類必須繼承自UIPopoverBackgroundView,而且子類必須覆寫父類中的一些列方法,示例以下:code
@interface MyView : UIPopoverBackgroundView @end @implementation MyView //這個方法返回箭頭寬度 + (CGFloat)arrowBase{ return 20; } //這個方法中返回內容視圖的偏移 +(UIEdgeInsets)contentViewInsets{ return UIEdgeInsetsMake(20, 20, 20, 20); } //這個方法返回箭頭高度 +(CGFloat)arrowHeight{ return 30; } //這個方法返回箭頭的方向 -(UIPopoverArrowDirection)arrowDirection{ return UIPopoverArrowDirectionUp; } //這個在設置箭頭方向時被調用 能夠監聽作處理 -(void)setArrowDirection:(UIPopoverArrowDirection)arrowDirection{ } //這個方法在設置箭頭偏移量時被調用 能夠監聽作處理 -(void)setArrowOffset:(CGFloat)arrowOffset{ } //重寫layout方法來來定義箭頭樣式 - (void)layoutSubviews { [super layoutSubviews]; CGSize arrowSize = CGSizeMake([[self class] arrowBase], [[self class] arrowHeight]); UIImage * image = [self drawArrowImage:arrowSize]; UIImageView * imageView = [[UIImageView alloc]initWithImage:image]; imageView.frame = CGRectMake(0, 0.0f, arrowSize.width, arrowSize.height); [self addSubview:imageView]; } //這個方法中進行背景色的設置 - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor redColor]; } return self; } - (instancetype)init { self = [super init]; if (self) { } return self; } //返回值決定是否渲染陰影 +(BOOL)wantsDefaultContentAppearance{ return NO; } //畫箭頭方法 - (UIImage *)drawArrowImage:(CGSize)size { UIGraphicsBeginImageContextWithOptions(size, NO, 0); CGContextRef ctx = UIGraphicsGetCurrentContext(); [[UIColor clearColor] setFill]; CGContextFillRect(ctx, CGRectMake(0.0f, 0.0f, size.width, size.height)); CGMutablePathRef arrowPath = CGPathCreateMutable(); CGPathMoveToPoint(arrowPath, NULL, (size.width/2.0f), 0.0f); CGPathAddLineToPoint(arrowPath, NULL, size.width, size.height); CGPathAddLineToPoint(arrowPath, NULL, 0.0f, size.height); CGPathCloseSubpath(arrowPath); CGContextAddPath(ctx, arrowPath); CGPathRelease(arrowPath); UIColor *fillColor = [UIColor yellowColor]; CGContextSetFillColorWithColor(ctx, fillColor.CGColor); CGContextDrawPath(ctx, kCGPathFill); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } @end
UIPopoverPresentationController是iOS8後系統新引入的控制器,其能夠很好的兼容iPhone與iPad。UIPopoverPresentationContriller的使用須要和UIViewController結合進行,使用過程示例以下:對象
UITableViewController tabCon = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain]; //設置跳轉模式爲popover模式 tabCon.modalPresentationStyle = UIModalPresentationPopover; //獲取到UIPopoverPresentationController對象 UIPopoverPresentationController* con = tabCon.popoverPresentationController; //設置彈出的基準視圖 con.sourceView = self.view; [self presentViewController:tabCon animated:YES completion:nil];
UIPopoverPresentationController中屬性以下:繼承
//設置代理 @property (nullable, nonatomic, weak) id <UIPopoverPresentationControllerDelegate> delegate; //設置容許的箭頭方向 @property (nonatomic, assign) UIPopoverArrowDirection permittedArrowDirections; //設置基準視圖或者區域 @property (nullable, nonatomic, strong) UIView *sourceView; @property (nonatomic, assign) CGRect sourceRect; //設置是否覆蓋基準視圖區域 @property (nonatomic, assign) BOOL canOverlapSourceViewRect NS_AVAILABLE_IOS(9_0); //設置基準BarButtonItem @property (nullable, nonatomic, strong) UIBarButtonItem *barButtonItem; //設置能夠進行用戶交互的視圖 @property (nullable, nonatomic, copy) NSArray<UIView *> *passthroughViews; //設置背景顏色 @property (nullable, nonatomic, copy) UIColor *backgroundColor; //設置Margin @property (nonatomic, readwrite) UIEdgeInsets popoverLayoutMargins; //設置自定義視圖 @property (nullable, nonatomic, readwrite, strong) Class <UIPopoverBackgroundViewMethods> popoverBackgroundViewClass;
UIPopoverPresentationControllerDelegate中的方法以下:get
//控制器將要彈出時調用 - (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController; //控制器將要消失時調用 - (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController; //控制器已經消失時調用 - (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController; //控制器接收到彈出消息時調用 - (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView * __nonnull * __nonnull)view;
專一技術,熱愛生活,交流技術,也作朋友。it
——琿少 QQ羣:203317592