• iOS:模态弹出窗控制器UIPopoverPresentationController


    模态弹出窗控制器: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(100, 200);
        
        //设置内容背景颜色
        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(100, 100, 40, 40)];
        
        //设置按钮背景颜色
        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];
        }
    }

     演示结果截图:

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

       

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

  • 相关阅读:
    optparse--强大的命令行参数处理包
    B/S和C/S架构的区别
    Document
    Document
    Document
    Document
    Document
    Document
    Document
    Document
  • 原文地址:https://www.cnblogs.com/XYQ-208910/p/4897511.html
Copyright © 2020-2023  润新知