UIPopoverPresentationController使用
UIPopoverPresentationController是什么?
iOS8.0之后引入的一个方便开发者创建带箭头的弹出控制器,类似qq消息页面点击右上角加号弹出的视图。继承UIPresentationController类,可用于iPhone和iPad ,比之前的UIPopoverController使用方便。下面给出实现的效果图:

UIPopoverPresentationController怎么使用?
UIPopoverPresentationControllerl类实例不需要直接创建,因为在UIViewController中有一个popoverPresentationController属性,可以从它获取。
这里给出基本的使用方法:
第一步:
创建一个UIViewController类的实例,最好采用他的子类,对它进行一些设置,这里我们设置了preferredContentSize属性和modalPresentationStyle,modalPresentationStyle一定要设置为UIModalPresentationPopover,而preferredContentSize可以不设置,不设置的话,系统会自动给一个几乎充满整个屏幕的视图。
ItemPopoverViewController *controller = [[ItemPopoverViewController alloc] init];
controller.preferredContentSize = CGSizeMake(, );
controller.modalPresentationStyle = UIModalPresentationPopover;
第二步:
对UIPopoverPresentationController进行设置,
首先看一下UIPopoverPresentationController中的一些属性,从下图可以看到sourceView和barButtonItem,sourceView是用于UIView类及其子类的,barButtonItem用于UIBarButtonItem,这两个属性是告诉UIPopoverPresentationController实例出现在哪个视图上,区别在于sourceView还需要指定sourceRect,否则pop出来的视图位置将不正确,一般我们给sourceRect赋当前视图的bounds属性就可以了。
@property (nullable, nonatomic, strong) UIView *sourceView;
@property (nonatomic, assign) CGRect sourceRect; // By default, a popover is not allowed to overlap its source view rect.
// When this is set to YES, popovers with more content than available space are allowed to overlap the source view rect in order to accommodate the content.
@property (nonatomic, assign) BOOL canOverlapSourceViewRect NS_AVAILABLE_IOS(9_0); @property (nullable, nonatomic, strong) UIBarButtonItem *barButtonItem;
现在看看demo里的代码
// 出现在UIBarButtonItem上面的
UIPopoverPresentationController *popController = [controller popoverPresentationController];
popController.delegate = self;
popController.barButtonItem = self.barItem;
// 出现在view上面的
UIPopoverPresentationController *popController = [controller popoverPresentationController];
popController.sourceView = self.button;
popController.sourceRect = self.button.bounds;
popController.delegate = self;
从上面代码可以看出这两种方式区别不大,注意到我们在这里指定了delegate属性,这是很重要的,下面将介绍它的作用。
第三步:
这是最后一步,控制器要显示,就需要用present的方法
[self presentViewController:controller animated:YES completion:nil];
同样的,要消失的话,调用下面这个方法
[self dismissViewControllerAnimated:YES completion:nil];
到这里,基本上我们已完成了所有内容,但是如果你看效果的话,会发现,并不是pop出一个视图,而是占据整个屏幕的视图,相信你还记得上面提到的代理,我们需要实现一个方法:
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}
加上这个之后,就能看到pop出来的效果了。
UIPopoverPresentationController显示内容
如果要显示内容,只需在相应视图控制器中添加内容即可,但是,因为一般我们都会设置preferredContentSize这个属性,设置了之后,显示的视图往往比较小,在viewDidLoad中控制器的根视图大小还不是我们设置的preferredContentSize这个值,不能用来做参考,创建的子视图的大小可以在viewWillLayoutSubviews或者viewDidLayoutSubviews中进行更新。
以上就是UIPopoverPresentationController基本用法.
demo下载地址:下载
UIPopoverPresentationController使用的更多相关文章
- iOS8新特性(1)-UIPopoverPresentationController使用
从iOS 8开始,苹果提出新的 UIPopoverPresentationController代替UIPopoverController: 新的UIPopoverPresentationControl ...
- iPad 控件 UIPopoverPresentationController 使用 iPhone可用
UIPopoverController 在iOS9之后被废弃了,,, iOS8 新控件UIPopoverPresentationController可运用在iphone和iPad上,使用基本同 UIP ...
- iOS:模态弹出窗控制器UIPopoverPresentationController
模态弹出窗控制器:UIPopoverPresentationController 实质:就是将内容控制器包装成PopoverPresentationController的形式,然后再模态出来,必须指定 ...
- iOS iOS8新特性--UIPopoverPresentationController
1.回顾UIPopoverController的使用,下面这份代码只能在ipad下运行 // 初始化控制器,SecondViewController类继承自UIViewController Secon ...
- UIPopoverPresentationController的使用
前言 最近项目中很多地方有一个相同的需求,那就是点击一个按钮的时候在按钮的某一个方向弹出一个视图,这个视图需要带有一个箭头指向,就像下图一样.要实现这个功能,就要用到UIPopoverPresenta ...
- 美团HD(3)-加载分类导航数据
DJHomeViewController.m /** 设置导航栏左侧内容 */ - (void)setupLeftNavItem { // Logo UIImageView *logoView = [ ...
- iPad编程
1. iPad 现有型号: iPad Pro, iPad Air, iPad mini 均配备Retina显示屏.早期还有iPad 依次对应的坐标系及分辨率: iPad Pro 坐标系:1366 x ...
- iOS--UIAlertView与UIAlertController和UIAlertAction之间的事儿
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备 ...
- popoverPresentationController UIPopoverController 使用方法详解
之前iPad特有的控件,现在iPhone亦可使用. 点击按钮,弹出popOverVC. 按钮的点击事件: - (IBAction)pickOrderAction:(UIButton *)sender ...
随机推荐
- 发送验证码(××s后重新发送)
html: <input class="tel" type="text" name="tel" placeholder="手 ...
- php中表单提交复选框与下拉列表项
在赶项目中,抽出半个小时来写篇博客吧,这个功能说实话不难,为什么要写呢,因为在复选框那里有小小的难点,我试了好多遍才试成功的,希望能为以后需要帮助的同学提供点思路. 先看一下我做的效果吧 就是给每个业 ...
- GitHub的实践
GitHub的实践 2017-05-08,晴,来小米已经一周的时间了,感谢领导能给我一周的时间来熟悉 ubuntu.spring boot.maven.docker.github .大家会问,这些不都 ...
- 细看JS中的BOM、DOM对象
DOM对象模型 DOM(Document Object Model),是指文档对象模型,是W3C组织推荐的处理可扩展标志语言的 ...
- IE haslayout
我们都知道浏览器有bug,而IE的bug似乎比大多数浏览器都多.IE的表现与其他浏览器不同的原因之一就是,显示引擎使用一个称为布局(layout)的内部概念. 因为布局是专门针对显示引擎内部工作方 ...
- Memcache服务搭建
Memcache Memcache的作用网上资料都讲的很好,说简单点就是减轻读取数据库的压力,原理也很简单: 被请求的数据会先到memcache里去取,如果没有就去数据库里取,顺便给memcache带 ...
- MyBatis之级联——一对多关系
上次我们讲到了MyBatis的一对一关系的表示,简单回顾一下一对一关系就是一个学生只有一个学生证.那么什么是一对多关系呢?一个学生有多个课程这就是一对多的关系.我们结合上一章中的学生和学生证,在此基础 ...
- let与const详解
在ES6中,js首次引入了块级作用域的概念,而什么是块级作用域? 众所就知,在js当中存在预解析的概念,就是变量提升.并且只存在全局作用域和私有作用域.在全局定义的变量就是全局变量,而在函数内部定义的 ...
- PHP中的函数声明与使用
函数 1. 函数名是标识符之一,只能有字母数字下划线,开头不能是数字: 函数名的命名,必须符合&quo ...
- 你对SpringMvc是如何理解的?
SpringMVC工作原理 SpringMvc是基于过滤器对servlet进行了封装的一个框架,我们使用的时候就是在web.xml文件中配置DispatcherServlet类:SpringMvc工作 ...