iOS中UIPopoverController使用详解

简介: iOS中UIPopoverController使用详解

一、引言

       UIPopoverController是Pad设备中常用的一种视图控制器,其在UI表现上为在当前视图控制器上面弹出一个子视图控制器,通常用来展示交互列表。示例如下图:

image.png

UIPopoverController只能用于iPad,在要兼容iPad和iPhone的项目中,需要根据设备类型使用两套代码。在iOS8之后,系统提供了UIPresentationController来代替她,UIPresentationController可以兼容iPhone与iPad。

二、UIPopoverController的使用详解

       首先UIPopoverController是一个容器控制器,其中需要承载一个ViewControler作为内容视图。UIPopoverController使用如下初始化方法创建:

//创建视图控制器的方法通过一个内容视图控制器创建

-(instancetype)initWithContentViewController:(UIViewController*)viewController;

创建出控制器后,调用如下方法可以将控制器弹出:

//这个方法将控制器以一个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)rectinView:(UIView*)viewpermittedArrowDirections:(UIPopoverArrowDirection)arrowDirectionsanimated:(BOOL)animated;

//以一个BarButtonItem为基准弹出其余参数意义同上

-(void)presentPopoverFromBarButtonItem:(UIBarButtonItem*)itempermittedArrowDirections:(UIPopoverArrowDirection)arrowDirectionsanimated:(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);

//这个属性用于自定义PopoverControllerUI展现传入自定义的背景视图类

@property(nullable, nonatomic,readwrite, strong) Class popoverBackgroundViewClass NS_AVAILABLE_IOS(5_0);

三、自定义UI展现的UIPopoverController

       通过设置UIPopoverController对象的popoverBacjgroundViewClass属性可以将一个自定义的类作为控制器的背景视图,需要注意,此自定义的类必须继承自UIPopoverBackgroundView,并且子类必须覆写父类中的一些列方法,示例如下:

@interfaceMyView: UIPopoverBackgroundView

@end

 

@implementationMyView

//这个方法返回箭头宽度

+ (CGFloat)arrowBase{

   return20;

}

//这个方法中返回内容视图的偏移

+(UIEdgeInsets)contentViewInsets{

   returnUIEdgeInsetsMake(20, 20, 20, 20);

}

//这个方法返回箭头高度

+(CGFloat)arrowHeight{

   return30;

}

//这个方法返回箭头的方向

-(UIPopoverArrowDirection)arrowDirection{

   returnUIPopoverArrowDirectionUp;

}

//这个在设置箭头方向时被调用可以监听做处理

-(void)setArrowDirection:(UIPopoverArrowDirection)arrowDirection{

   

}

//这个方法在设置箭头偏移量时被调用可以监听做处理

-(void)setArrowOffset:(CGFloat)arrowOffset{

   

}

//重写layout方法来来定义箭头样式

- (void)layoutSubviews

{

   [superlayoutSubviews];

   CGSize arrowSize = CGSizeMake([[selfclass] arrowBase], [[selfclass] arrowHeight]);

   UIImage * image  = [selfdrawArrowImage:arrowSize];

   UIImageView * imageView = [[UIImageView alloc]initWithImage:image];

   imageView.frame = CGRectMake(0, 0.0f, arrowSize.width, arrowSize.height);

   [selfaddSubview:imageView];

}

//这个方法中进行背景色的设置

- (instancetype)initWithFrame:(CGRect)frame

{

   self= [superinitWithFrame:frame];

   if(self) {

       self.backgroundColor = [UIColor redColor];

   }

   returnself;

}

- (instancetype)init

{

   self= [superinit];

   if(self) {

       

   }

   returnself;

}

//返回值决定是否渲染阴影

+(BOOL)wantsDefaultContentAppearance{

   returnNO;

}

//画箭头方法

- (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();

   returnimage;

}

@end

四、UIPopoverPresentationController应用解析

   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;

[selfpresentViewController:tabCon animated:YEScompletion: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中的方法如下:

//控制器将要弹出时调用

- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController;

//控制器将要消失时调用

- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController;

//控制器已经消失时调用

- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController;

//控制器接收到弹出消息时调用

- (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inoutCGRect *)rect inView:(inoutUIView  * __nonnull* __nonnull)view;

 

UIPopoverController只能用于iPad,在要兼容iPad和iPhone的项目中,需要根据设备类型使用两套代码。在iOS8之后,系统提供了UIPresentationController来代替她,UIPresentationController可以兼容iPhone与iPad。

二、UIPopoverController的使用详解

       首先UIPopoverController是一个容器控制器,其中需要承载一个ViewControler作为内容视图。UIPopoverController使用如下初始化方法创建:

//创建视图控制器的方法通过一个内容视图控制器创建

-(instancetype)initWithContentViewController:(UIViewController*)viewController;

创建出控制器后,调用如下方法可以将控制器弹出:

//这个方法将控制器以一个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)rectinView:(UIView*)viewpermittedArrowDirections:(UIPopoverArrowDirection)arrowDirectionsanimated:(BOOL)animated;

//以一个BarButtonItem为基准弹出其余参数意义同上

-(void)presentPopoverFromBarButtonItem:(UIBarButtonItem*)itempermittedArrowDirections:(UIPopoverArrowDirection)arrowDirectionsanimated:(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);

//这个属性用于自定义PopoverControllerUI展现传入自定义的背景视图类

@property(nullable, nonatomic,readwrite, strong) Class popoverBackgroundViewClass NS_AVAILABLE_IOS(5_0);

三、自定义UI展现的UIPopoverController

       通过设置UIPopoverController对象的popoverBacjgroundViewClass属性可以将一个自定义的类作为控制器的背景视图,需要注意,此自定义的类必须继承自UIPopoverBackgroundView,并且子类必须覆写父类中的一些列方法,示例如下:

@interfaceMyView: UIPopoverBackgroundView

@end

 

@implementationMyView

//这个方法返回箭头宽度

+ (CGFloat)arrowBase{

   return20;

}

//这个方法中返回内容视图的偏移

+(UIEdgeInsets)contentViewInsets{

   returnUIEdgeInsetsMake(20, 20, 20, 20);

}

//这个方法返回箭头高度

+(CGFloat)arrowHeight{

   return30;

}

//这个方法返回箭头的方向

-(UIPopoverArrowDirection)arrowDirection{

   returnUIPopoverArrowDirectionUp;

}

//这个在设置箭头方向时被调用可以监听做处理

-(void)setArrowDirection:(UIPopoverArrowDirection)arrowDirection{

   

}

//这个方法在设置箭头偏移量时被调用可以监听做处理

-(void)setArrowOffset:(CGFloat)arrowOffset{

   

}

//重写layout方法来来定义箭头样式

- (void)layoutSubviews

{

   [superlayoutSubviews];

   CGSize arrowSize = CGSizeMake([[selfclass] arrowBase], [[selfclass] arrowHeight]);

   UIImage * image  = [selfdrawArrowImage:arrowSize];

   UIImageView * imageView = [[UIImageView alloc]initWithImage:image];

   imageView.frame = CGRectMake(0, 0.0f, arrowSize.width, arrowSize.height);

   [selfaddSubview:imageView];

}

//这个方法中进行背景色的设置

- (instancetype)initWithFrame:(CGRect)frame

{

   self= [superinitWithFrame:frame];

   if(self) {

       self.backgroundColor = [UIColor redColor];

   }

   returnself;

}

- (instancetype)init

{

   self= [superinit];

   if(self) {

       

   }

   returnself;

}

//返回值决定是否渲染阴影

+(BOOL)wantsDefaultContentAppearance{

   returnNO;

}

//画箭头方法

- (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();

   returnimage;

}

@end

四、UIPopoverPresentationController应用解析

   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;

[selfpresentViewController:tabCon animated:YEScompletion: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中的方法如下:

//控制器将要弹出时调用

- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController;

//控制器将要消失时调用

- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController;

//控制器已经消失时调用

- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController;

//控制器接收到弹出消息时调用

- (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inoutCGRect *)rect inView:(inoutUIView  * __nonnull* __nonnull)view;

 

目录
相关文章
|
iOS开发 数据可视化
|
6天前
|
IDE Android开发 iOS开发
探索Android与iOS开发的差异:平台选择对项目成功的影响
【9月更文挑战第27天】在移动应用开发的世界中,Android和iOS是两个主要的操作系统平台。每个系统都有其独特的开发环境、工具和用户群体。本文将深入探讨这两个平台的关键差异点,并分析这些差异如何影响应用的性能、用户体验和最终的市场表现。通过对比分析,我们将揭示选择正确的开发平台对于确保项目成功的重要作用。
|
14天前
|
开发框架 数据可视化 Java
iOS开发-SwiftUI简介
iOS开发-SwiftUI简介
|
2天前
|
开发框架 移动开发 Android开发
安卓与iOS开发中的跨平台解决方案:Flutter入门
【9月更文挑战第30天】在移动应用开发的广阔舞台上,安卓和iOS两大操作系统各自占据半壁江山。开发者们常常面临着选择:是专注于单一平台深耕细作,还是寻找一种能够横跨两大系统的开发方案?Flutter,作为一种新兴的跨平台UI工具包,正以其现代、响应式的特点赢得开发者的青睐。本文将带你一探究竟,从Flutter的基础概念到实战应用,深入浅出地介绍这一技术的魅力所在。
18 7
|
6天前
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台解决方案
【9月更文挑战第27天】在移动应用开发的广阔天地中,安卓和iOS两大操作系统如同双子星座般耀眼。开发者们在这两大平台上追逐着创新的梦想,却也面临着选择的难题。如何在保持高效的同时,实现跨平台的开发?本文将带你探索跨平台开发的魅力所在,揭示其背后的技术原理,并通过实际案例展示其应用场景。无论你是安卓的忠实拥趸,还是iOS的狂热粉丝,这篇文章都将为你打开一扇通往跨平台开发新世界的大门。
|
12天前
|
前端开发 iOS开发 开发者
探索iOS开发中的SwiftUI框架
【9月更文挑战第21天】在iOS应用开发的广阔天地中,SwiftUI框架如一股清新之风,为开发者带来了声明式语法的便捷与高效。本文将深入探讨SwiftUI的核心概念、布局方式及数据绑定机制,同时通过实例演示如何运用SwiftUI构建用户界面,旨在引领读者领略SwiftUI的魅力,并激发其对iOS开发新趋势的思考与实践。
31 6
|
12天前
|
安全 Swift iOS开发
探索iOS开发之旅:Swift语言的魅力与挑战
【9月更文挑战第21天】在这篇文章中,我们将一起潜入iOS开发的海洋,探索Swift这门现代编程语言的独特之处。从简洁的语法到强大的功能,Swift旨在让开发者能够以更高效、更安全的方式构建应用程序。通过实际代码示例,我们会深入了解Swift如何简化复杂任务,并讨论它面临的挑战和未来的发展方向。无论你是初学者还是有经验的开发者,这篇文章都将为你提供新的视角和知识。
28 4
|
22天前
|
IDE 开发工具 Android开发
安卓与iOS开发对比:平台选择对项目成功的影响
【9月更文挑战第10天】在移动应用开发的世界中,选择正确的平台是至关重要的。本文将深入探讨安卓和iOS这两大主要移动操作系统的开发环境,通过比较它们的市场份额、开发工具、编程语言和用户群体等方面,为开发者提供一个清晰的指南。我们将分析这两个平台的优势和劣势,并讨论如何根据项目需求和目标受众来做出最佳选择。无论你是初学者还是有经验的开发者,这篇文章都将帮助你更好地理解每个平台的特性,并指导你做出明智的决策。