模态弹出窗控制器: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. 20145120 《Java程序设计》实验三实验报告

    20145120 <Java程序设计>实验三实验报告 实验名称:敏捷开发与XP实践 实验目的与要求: XP基础 XP核心实践 相关工具 (一)敏捷开发与XP 极限编程(eXtreme Pr ...

  2. struts1 和 struts2中Action什么时候实例化

    精帖1:http://blog.csdn.net/lfsf802/article/details/7277013 精帖1:http://blog.csdn.net/wmj2003/article/de ...

  3. CSS3 filter10种特效整理

    -webkit-filter是css3的一个属性,Webkit率先支持了这几个功能,感觉效果很不错.一共有10种最基本的特效,下来这个DEMO很好的展示了这些效果: <!DOCTYPE html ...

  4. js判断浏览器滚动条是否拉到底

    $(window).scroll(function(){ // 当滚动到最底部以上n像素时, 加载新内容 if ($(document).height() - $(this).scrollTop() ...

  5. win8.1 cygwin编译java轻量虚拟机avian

    1.背景 昨天在网上看到别人用aauto写本地小程序写的很爽,我觉得如果java的jre能小一点,凭借java庞大的第三方类库写小工具也还算不错的.本人就经常用eclipse+一些commons包写些 ...

  6. html5之canvas练习

    代码地址:github.com/peng666/blogs 在线地址:http://peng666.github.io/blogs/

  7. python 实现斐波那契数列

    def fib(n): a,b=0,1 while a<n: print(a,end=" ") a,b=b,a+b print() fib(2000) 输出: 0 1 1 2 ...

  8. mysql--乱码

    不知道为什么utf8反而会乱码,每次都是设gbk,,唉这样写项目的时候也是有点问题的T  T set names gbk; 版权声明:本文为博主原创文章,未经博主允许不得转载.

  9. JS 学习笔记--5---对象和数组

    1.Object类型(引用类型) 不具备多少功能,但是对于在ECMAScript中存储和传递数据确实,确是很理想的选择. 创建方式:(1).使用new Object();方式创建对象,然后对对象进行设 ...

  10. 3-Highcharts 3D图之3D柱状图分组叠堆3D图

    <!DOCTYPE> <html lang='en'> <head> <title>3-Highcharts 3D图之3D柱状图分组叠堆3D图</ ...