前言

最近项目中很多地方有一个相同的需求,那就是点击一个按钮的时候在按钮的某一个方向弹出一个视图,这个视图需要带有一个箭头指向,就像下图一样。要实现这个功能,就要用到UIPopoverPresentationController这个类了。

 
WechatIMG38.jpeg

简介

一个带有箭头的弹出视图从出现到消失的整个过程,都是UIPopoverPresentationController类的实例在管理,UIPopoverPresentationController类的实例管理着弹出视图的外形和其它的一些性质。

我们不需要直接去创建这个类的对象,当我们把这个弹出视图对应的视图控制器的modalPresentationStyle属性设置为UIModalPresentationPopover时,viewcontroller就会拥有一个popoverPresentationController属性,它就是UIPopoverPresentationController类型的,用它来管理这个弹出视图的外形和其它一些性质。

UIPopoverPresentationControllerDelegate

UIPopoverPresentationControllerDelegate对象会通知它的delegate整个弹出视图从弹出到消失的整个过程。下面是其主要的API:
通知delegate视图将要呈现:

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

询问delegate视图是否应该消失

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

通知delegate视图已经消失

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

相关属性

 
3E798BC1-2CF1-4634-8859-9EA7515BE1E4.png
  • backgroundColor
    上图中设置的backgroundColor是红色,上图中弹出视图是由两部分叠加的,一部分是上面的viewcontroller的View,其背景色是黄色,还有一部分是承载viewcontroller的View的视图,这个视图是由viewcontroller的popoverPresentationController对象来管理的。
  • passthroughViews
    这是一个数组,里面装着视图,一般情况下在弹出视图可见的情况下,点击界面上其他视图是无效的,这会导致弹出视图消失,但是这个数组中的视图不会这样,在弹出视图可见的情况下仍然可以点击这些视图。
  • canOverlapSourceViewRect
    这是一个布尔值,说明弹出视图是否可以和sourceRect重叠。
  • sourceView
    这个属性其实就是要指出弹出视图的箭头要指向哪个视图。
  • permittedArrowDirections
    这个属性要确定弹出视图的箭头的方法,它是一个枚举值,上图中设置的箭头方向是左边。
  • sourceRect
    有了sourceView和permittedArrowDirections还不能完全确定箭头的位置,还需要一个参数,这个参数就是sourceRect。上图中设置的sourceRect为_button.bnounds,当我们把sourceRect设置为CGRectMake(0, 0, _button.bounds.size.width, _button.bounds.size.height / 2.0);也即是这个按钮的上半区域的时候,我们看一下效果:
     
    0D46A23E-3BF1-4241-8A06-1BB92235F068.png

简单应用

  1. TestViewController *testVC = [[TestViewController alloc] init];
  2. testVC.preferredContentSize = CGSizeMake(150, 150);
  3. testVC.modalPresentationStyle = UIModalPresentationPopover;
  4. testVC.popoverPresentationController.delegate = self;
  5. testVC.popoverPresentationController.sourceView = _button;
  6. testVC.popoverPresentationController.sourceRect = CGRectMake(0, 0, _button.bounds.size.width / 2.0, _button.bounds.size.height / 2.0);
  7. testVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionLeft;
  8. testVC.popoverPresentationController.backgroundColor = [UIColor redColor];
  9. testVC.popoverPresentationController.canOverlapSourceViewRect = NO;
  10. [self presentViewController:testVC animated:YES completion:^{
  11. }];

注意,这样做的话是无论如何都不能成功显示弹出视图的,我们还需要实现代理的一个方法:

  1. #pragma mark - <UIPopoverPresentationControllerDelegate>
  2. - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
  3. return UIModalPresentationNone;
  4. }

这样就能成功实现了。

UIPopoverPresentationController的使用的更多相关文章

  1. iOS8新特性(1)-UIPopoverPresentationController使用

    从iOS 8开始,苹果提出新的 UIPopoverPresentationController代替UIPopoverController: 新的UIPopoverPresentationControl ...

  2. iPad 控件 UIPopoverPresentationController 使用 iPhone可用

    UIPopoverController 在iOS9之后被废弃了,,, iOS8 新控件UIPopoverPresentationController可运用在iphone和iPad上,使用基本同 UIP ...

  3. iOS:模态弹出窗控制器UIPopoverPresentationController

    模态弹出窗控制器:UIPopoverPresentationController 实质:就是将内容控制器包装成PopoverPresentationController的形式,然后再模态出来,必须指定 ...

  4. iOS iOS8新特性--UIPopoverPresentationController

    1.回顾UIPopoverController的使用,下面这份代码只能在ipad下运行 // 初始化控制器,SecondViewController类继承自UIViewController Secon ...

  5. UIPopoverPresentationController使用

    UIPopoverPresentationController是什么? iOS8.0之后引入的一个方便开发者创建带箭头的弹出控制器,类似qq消息页面点击右上角加号弹出的视图. UIPopoverPre ...

  6. 美团HD(3)-加载分类导航数据

    DJHomeViewController.m /** 设置导航栏左侧内容 */ - (void)setupLeftNavItem { // Logo UIImageView *logoView = [ ...

  7. iPad编程

    1. iPad 现有型号: iPad Pro, iPad Air, iPad mini 均配备Retina显示屏.早期还有iPad 依次对应的坐标系及分辨率: iPad Pro 坐标系:1366 x ...

  8. iOS--UIAlertView与UIAlertController和UIAlertAction之间的事儿

      iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备 ...

  9. popoverPresentationController UIPopoverController 使用方法详解

    之前iPad特有的控件,现在iPhone亦可使用. 点击按钮,弹出popOverVC. 按钮的点击事件: - (IBAction)pickOrderAction:(UIButton *)sender ...

随机推荐

  1. vscode环境配置(一)——C Program运行

    ctrl + shift +p 打开应用商店 搜索 C/C++  和 Code Runner(一键编译运行)  

  2. Mac 软件包管理器Homebrew使用指北

    Homebrew Homebrew由开发者 Max Howell 开发,并基于 BSD 开源,是一个非常方便的软件包包管理器工具. Homebrew 官网 Homebrew 的几个核心概念 在正式介绍 ...

  3. ArrayList简介

    ArrayList简介 ArrayList以数组为底层数据结构的集合,是一个动态的数组队列,就是说该类的容量可以增长,与一般的数组不同. public class ArrayList<E> ...

  4. [C#] [VS] Snippets快捷代码块之 Region

    代码长了,阅读起来不方便, 于是,C#中我们经常会用 region来折叠代码块. 在VS中,输入 #region , 点Tab,会自动生成如下: #region MyRegion #endregion ...

  5. 函数:exit()

    函数名: exit() 所在头文件:stdlib.h(如果是"VC6.0"的话头文件为:windows.h) 功 能: 关闭所有文件,终止正在执行的进程. exit(1)表示异常退 ...

  6. [Python基础]002.语法(1)

    语法(1) 变量 基本数据类型 空值 布尔值 数字 字符串 列表 元组 字典 结构嵌套 变量 定义变量 i = 10 这样就定义了一个名为 i 的变量,它的值是 10 . 变量名必须是大小写英文.数字 ...

  7. [工具推荐]003.Tortoisegit使用教程(补充)

    前文介绍:       在前文<[工具推荐]003.Tortoisegit使用教程>中详细介绍了如何使用Tortoisegit的使用,但是大家使用后反映一点,就是每次操作都需要输入账号名和 ...

  8. nacos 配置

    具体请访问 https://nacos.io/zh-cn/docs/what-is-nacos.html 网站查看文档,现在开始使用Nacos. 1. 下载Nacos源码 Nacos可以通过 http ...

  9. 动态ip服务器 动态ip服务器的连接方式

    动态IP服务器指的是在需要的时候才进行随机IP地址分配的服务器.所谓动态就是指当你每一次上网时,电信会随机分配一个IP地址,服务器作为我们设为的大脑,拥有自己独立的账户及密码,通过远程连接动态IP服务 ...

  10. Unity 游戏框架搭建 2019 (五十六/五十七) 需求分析-架构中最重要的一环&从 EmptyGO 到 Manager Of Managers

    我们的项目开始立项的时候,最常见的一个情况就是:几个人的小团队,一开始什么也不做,就开始写代码,验证逻辑,游戏就开始写起来了.而公司的一些所谓的领导层面一开始就把游戏定义为我们要做一个大作.这个事情本 ...