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 ...
随机推荐
- 【翻译】FreeMarker——入门
原文传送门 1. Template + data-model = output data-model是一个树状模型,通常是一个java对象. 2.data-model 入门 hashes(散列):目录 ...
- PHP学习笔记-4(时间戳)
在学习PHP时间戳的时候,发现了一个有趣的现象,就是发现用strtotime()这个函数返回的时间戳跟人家的不一样,以为是自己哪里写错了,后来发现不是这样的. 是因为设置的时区不同,从而导致了时间显示 ...
- Codeforces Round #410 (Div. 2)D题
D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Hadoop技术在商业智能BI中的应用
Hadoop是个很流行的分布式计算解决方案,是Apache的一个开源项目名称,核心部分包括HDFS及MapReduce.其中,HDFS是分布式文件系统,MapReduce是分布式计算引擎.时至今日,H ...
- 设计模式的征途—6.建造者(Builder)模式
建造者模式又称为生成器模式,它是一种较为复杂.使用频率也相对较低的创建型模式.建造者模式为客户端返回的不是一个简单的产品,而是一个由多个部件组成的复杂产品.因为,没有人买车会只买一个方向盘或者轮胎,大 ...
- MySQL使用pt-online-change-schema工具在线修改1.6亿级数据表结构
摘 要:本文阐述了MySQL DDL 的问题现状.pt-online-schema-change的工作原理,并实际利用pt-online-schema-change工具在线修改生产环境下1.6亿级数 ...
- Java学习笔记——封装、继承和多态
先说说封装: 用new 一条狗来举个例子: public class Dog { //私有化字段 private String name; private int age; //无参构造 Dog(){ ...
- Oracle数据库悲观锁与乐观锁详解
数据的锁定分为两种方法,第一种叫做悲观锁,第二种叫做乐观锁.什么叫悲观锁呢,悲观锁顾名思义,就是对数据的冲突采取一种悲观的态度,也就是说假设数据肯定会冲突,所以在数据开始读取的时候就把数据锁定住.而乐 ...
- PreparedStatement/Statement处理insert update等操作时乱码,以及URL
原文: 在顶目中无意中碰到PreparedStatement 在存DB时出现乱码,困扰了好久终于解决问题 问题代码如下 ps = con.prepareStatement(INSERT_SQL); p ...
- Eclipse Java 关联源码
今天打代码的时候打算看看Java的源码是怎么实现的 没想到还没关联源码 遇到上面的情况只需要关联下源码就可以对着方法按F3查看JAVA的开源代码. 解决上面如下: 找到jdk的安装目录 找到src.z ...