首页 技术 正文
技术 2022年11月20日
0 收藏 678 点赞 5,150 浏览 2615 个字

UIPopoverPresentationController是什么?

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

UIPopoverPresentationController使用

UIPopoverPresentationController怎么使用?

UIPopoverPresentationControllerl类实例不需要直接创建,因为在UIViewController中有一个popoverPresentationController属性,可以从它获取。

这里给出基本的使用方法:

第一步:

创建一个UIViewController类的实例,最好采用他的子类,对它进行一些设置,这里我们设置了preferredContentSize属性和modalPresentationStyle,modalPresentationStyle一定要设置为UIModalPresentationPopover,而preferredContentSize可以不设置,不设置的话,系统会自动给一个几乎充满整个屏幕的视图。

ItemPopoverViewController *controller = [[ItemPopoverViewController alloc] init];
controller.preferredContentSize = CGSizeMake(, );
controller.modalPresentationStyle = UIModalPresentationPopover;

第二步:

对UIPopoverPresentationController进行设置,

首先看一下UIPopoverPresentationController中的一些属性,从下图可以看到sourceViewbarButtonItem,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下载地址:下载

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,990
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,504
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,348
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,133
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,765
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,843