模态弹出窗控制器:UIPopoverPresentationController

实质:就是将内容控制器包装成PopoverPresentationController的形式,然后再模态出来,必须指定来源视图及其frame区域,也即指向谁。

功能:它也是一个弹出窗控制器,它在iOS8中替代了UIPopoverController,它在功能上与旧的controller完全等同,并且新增了一些内置的适配特性,可以自动适配iPad与iPhone。当然它也需要一个继承于UIViewController的控制器作为内容控制器,然后模态它的窗口,即显示弹出窗。

枚举:模态显示类型

typedef NS_ENUM(NSInteger, UIModalPresentationStyle) {

UIModalPresentationFullScreen = 0,

UIModalPresentationPageSheet ,

UIModalPresentationFormSheet ,

UIModalPresentationCurrentContext ,

UIModalPresentationCustom ,

UIModalPresentationOverFullScreen ,

UIModalPresentationOverCurrentContext ,

UIModalPresentationPopover , //模态显示弹出窗

UIModalPresentationNone  = -1,

};

模态弹出窗控制器介绍:

@interface UIPopoverPresentationController : UIPresentationController

属性:

//模态弹出窗控制器代理

@property (nonatomic, assign) id <UIPopoverPresentationControllerDelegate> delegate;

//弹出窗箭头方向

@property (nonatomic, assign) UIPopoverArrowDirection permittedArrowDirections;

//弹出窗显示的视图资源

@property (nonatomic, retain) UIView *sourceView;

//弹出窗显示的区域

@property (nonatomic, assign) CGRect sourceRect;

//工具条按钮

@property (nonatomic, retain) UIBarButtonItem *barButtonItem;

//弹出窗箭头方向(只读)

@property (nonatomic, readonly) UIPopoverArrowDirection arrowDirection;

//过滤视图控件,设置不可与用户做交互

@property (nonatomic, copy) NSArray *passthroughViews;

//弹出窗背景颜色

@property (nonatomic, copy) UIColor *backgroundColor;

//弹出窗偏移位置

@property (nonatomic, readwrite) UIEdgeInsets popoverLayoutMargins;

//弹出窗背景视图的类

@property (nonatomic, readwrite, retain) Class <UIPopoverBackgroundViewMethods> popoverBackgroundViewClass;

@end

协议:

@protocol UIPopoverPresentationControllerDelegate

@optional

//模态弹出窗窗口时触发的方法,可以进行数据传输

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

//将要关闭弹出窗窗口时触发的方法

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

//已经关闭弹出窗窗口时触发的方法

- (void)popoverPresentationControllerDidDismissPopover (UIPopoverPresentationController*) popoverPresentationController;

//弹出窗将要复位到指定视图区域时触发的方法

- (void)popoverPresentationController:(UIPopoverPresentationController*)popoverPresentationController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView**)view;

@end

视图控制器类

@interface UIViewController

//设置内容控制器大小

@property (nonatomic) CGSize preferredContentSize

//模态显示类型

@property(nonatomic,assign) UIModalPresentationStyle modalPresentationStyle

@end

视图控制器分类(适配显示)

@interface UIViewController (UIAdaptivePresentations)

//管理模态窗口的显示控制器(presentingViewController、presentedViewController)

@property (nonatomic,readonly) UIPresentationController *presentationController;

//模态弹出窗控制器

@property (nonatomic,readonly) UIPopoverPresentationController *popoverPresentationController ;

@end

演示实例如下:

1.创建一个继承自UIViewController的内容控制器ContentViewController:

2.需要的文件截图为:

3.在内容控制器ContentViewController.m文件中设置模态弹出窗的大小和背景颜色

- (void)viewDidLoad {
[super viewDidLoad]; //设置内容区域大小
self.preferredContentSize = CGSizeMake(, ); //设置内容背景颜色
self.view.backgroundColor = [UIColor blueColor];
}

4.在ViewController.m文件中操作的代码如下:

//声明必要的属性

@interface ViewController ()
@property (strong,nonatomic)UIPopoverPresentationController *popoverPresentVC;//声明模态弹出窗控制器
@property (strong,nonatomic)UIPopoverController *popoverVC; //声明弹出窗控制器
@property (strong,nonatomic)ContentViewController *contentVC; //声明内容控制器
@end

//创建按钮,并添加按钮事件,用来打开模态弹出窗

    //创建按钮
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(, , , )]; //设置按钮背景颜色
button.backgroundColor = [UIColor purpleColor]; //设置按钮标题
[button setTitle:@"打开" forState:UIControlStateNormal]; //添加按钮事件
[button addTarget:self action:@selector(Open:) forControlEvents:UIControlEventTouchUpInside]; //将按钮添加到视图中
[self.view addSubview:button];

//实现按钮事件

#pragma mark -打开弹窗(将内容控制器以模态窗口的形式打开和关闭)
-(void)Open:(UIButton *)sender
{
if (!self.popoverVC.isPopoverVisible)
{
//创建内容控制器
self.contentVC = [[ContentViewController alloc]init]; //设置内容控制器模态显示类型为模态弹出窗
self.contentVC.modalPresentationStyle = UIModalPresentationPopover; //创建popoverPresentVC模态弹出窗控制器,包装内容控制器
self.popoverPresentVC = self.contentVC.popoverPresentationController; //设置一直显示后,即使点击按钮,此时弹窗也关闭不了,所以不这么设置
//[self.popoverPresentVC setPassthroughViews:@[self.view,sender]]; //设置模态弹窗的显示区域
self.popoverPresentVC.sourceView = sender;
self.popoverPresentVC.sourceRect = sender.bounds; //设置箭头方向自适应
self.popoverPresentVC.permittedArrowDirections = UIPopoverArrowDirectionAny; //打开模态窗口
[self presentViewController:self.contentVC animated:YES completion:nil];
}
else
{
//关闭模态弹窗
[self.contentVC dismissViewControllerAnimated:YES completion:nil];
}
}

演示结果截图:

开始时:                                                 点击按钮时,模态出内容弹出窗:

   

在点击屏幕中任何一处时,都可以关闭模态的弹出窗:

iOS:模态弹出窗控制器UIPopoverPresentationController的更多相关文章

  1. iOS:弹出窗控制器:UIPopoverController

    弹出窗控制器:UIPopoverController 截图:   实质:就是将内容控制器包装成popoverController的形式,然后在模态出来,必须给定指向目标(target.frame). ...

  2. Bootstrap模态弹出窗

    Bootstrap模态弹出窗有三种方式: 1.href触发模态弹出窗元素: <a class="btn btn-primary" data-toggle="moda ...

  3. data-参数说明(模态弹出窗的使用)

    除了通过data-toggle和data-target来控制模态弹出窗之外,Bootstrap框架针对模态弹出框还提供了其他自定义data-属性,来控制模态弹出窗.比如说:是否有灰色背景modal-b ...

  4. bootstrap-data-target触发模态弹出窗元素的data使用 data-toggle与data-target的作用 深入ASP.NET MVC之九:Ajax支持 Asp.Net MVC4系列--进阶篇之AJAX

    bootstrap-data-target触发模态弹出窗元素的data使用 时间:2017-05-27 14:22:34      阅读:4479      评论:0      收藏:0      [ ...

  5. iOS模态弹出半透明视图控制器

    项目中需要实现点击按钮出现的视图全屏覆盖,呈半透明状态可以看到下面的视图? 解决方案: 绕了很多弯路原来可以使用模态弹出一个视图控制器 在iOS8之后只需要设置一个最新的属性 SecondViewCo ...

  6. bootstrap学习--模态弹出框modals轮子

    1.点击按钮型 <link rel="stylesheet" href="lib/bootstrap/css/bootstrap.min.css"> ...

  7. Bootstrap模态弹出框

    前面的话 在 Bootstrap 框架中把模态弹出框统一称为 Modal.这种弹出框效果在大多数 Web 网站的交互中都可见.比如点击一个按钮弹出一个框,弹出的框可能是一段文件描述,也可能带有按钮操作 ...

  8. 玩转Bootstrap(JS插件篇)-第1章 模态弹出框 :1-4 模态弹出框--结构分析

    模态弹出框--结构分析 Bootstrap框架中的模态弹出框,分别运用了“modal”.“modal-dialog”和“modal-content”样式,而弹出窗真正的内容都放置在“modal-con ...

  9. 玩转Bootstrap(JS插件篇)-第1章 模态弹出框 :1-3 模态弹出框

    模态弹出框(Modals) 这一小节我们先来讲解一个“模态弹出框”,插件的源文件:modal.js. 右侧代码编辑器(30行)就是单独引入 bootstrap 中发布出的“modal.js”文件. 样 ...

随机推荐

  1. public、protect、private在父类子类中使用

    先贴出一张,直观的.估计大家都见过的关于public.protect.private的范围图 作用域 当前类 同一package 子孙类 其他package public     T         ...

  2. MVC缓存技术

    一.MVC缓存简介 缓存是将信息(数据或页面)放在内存中以避免频繁的数据库存储或执行整个页面的生命周期,直到缓存的信息过期或依赖变更才再次从数据库中读取数据或重新执行页面的生命周期.在系统优化过程中, ...

  3. Careercup - Google面试题 - 6283958983589888

    2014-05-06 11:31 题目链接 原题: Find the k-th Smallest Element in Two Sorted Arrays. I followed the algori ...

  4. UVALive - 6952 Cent Savings dp

    题目链接: http://acm.hust.edu.cn/vjudge/problem/116998 Cent Savings Time Limit: 3000MS 问题描述 To host a re ...

  5. hdu 4240 Route Redundancy 最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4240 A city is made up exclusively of one-way steets. ...

  6. JAVA 显示图片的简单源码 分类: Java Game 2014-08-14 10:10 77人阅读 评论(0) 收藏

    此代码的原理就是用JLabel来加载图片,再将JLabel放入JFrame中, package com.mywork; import javax.swing.ImageIcon; import jav ...

  7. A trip through the graphics pipeline 2011 Part 10(翻译)

    之前的几篇翻译都烂尾了,这篇希望....能好些,恩,还有往昔呢. ------------------------------------------------------------- primi ...

  8. ActiveMQ主从配置

    这种方式有个问题,activemq1有消息没消费完但是突然宕机,虽然程序会自动连到activemq2.但是activemq1的消息只有等机器恢复后才会被消费. 1.启动:我这里使用的是apache-a ...

  9. [工作积累] Android system dialog with native callback

    JNI: invoke java dialog with native callback: store native function address in java, and invoke nati ...

  10. switch语句的使用,非常好

    这是谭浩强课本上枚举类型的例子,但是我贴这个例子的代码不是因为枚举类型,是因为这个代码使用switch语句用得非常好,值得一贴. 题目是这样的:有红.黄.蓝.白.黑5中颜色的球若干,依次取出3个球,求 ...