美文网首页iOS Developer
UIPopoverPresentationController

UIPopoverPresentationController

作者: DeadRabbit | 来源:发表于2017-07-11 18:54 被阅读3234次

    前言

    学习的时候遗漏了这个知识点,最近在做一个类微信、支付宝的 "+"号弹出浮窗的功能,发现了这个好用的东西~ 做个记录方便日后查阅
    NS_CLASS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED //iOS8之后可用 @interface UIPopoverPresentationController : UIPresentationController

    基本使用


    效果图
    点击pop按钮后触发的Action代码如下
    /* Present the view controller using the popover style. */
    
    // 每个viewController,都有一个modalPresentationStyle属性
    TestViewController * test = [[TestViewController alloc]init];
    test.preferredContentSize = CGSizeMake(300, 200);//设置浮窗的宽高
    test.modalPresentationStyle = UIModalPresentationPopover;
     
     /* Get the popover presentation controller and configure it. */
    
    //获取TestViewController的UIPopoverPresentationController
    UIPopoverPresentationController * popover = [test popoverPresentationController];
    popover.delegate = self;
    popover.permittedArrowDirections = UIPopoverArrowDirectionUp;//设置箭头位置
    popover.sourceView = self.popViewButton;//设置目标视图
    popover.sourceRect = self.popViewButton.bounds;//弹出视图显示位置
    popover.backgroundColor = [UIColor redColor];//设置弹窗背景颜色(效果图里红色区域)
    [self presentViewController:test animated:YES completion:nil];
    

    ** UIPopoverPresentationControllerDelegate代理方法 **

    /* 
    For iOS8.0, the only supported adaptive presentation styles 
    are UIModalPresentationFullScreen and UIModalPresentationOverFullScreen. */
    // 设置 浮窗弹窗的推出样式,效果图中设置style为UIModalPresentationNone
    -(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:
    (UIPresentationController *)controller;
    
    // Called on the delegate when the popover controller will dismiss the popover. Return NO to prevent the
    // dismissal of the view.
    // 点击浮窗背景popover controller是否消失
    - (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController;
    
    // Called on the delegate when the user has taken action to dismiss the popover. This is not called when the popover is dimissed programatically.
    // 浮窗消失时调用
    - (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController;
    
    
    

    防止点击UIPopoverController区域外消失

    默认情况下
    只要UIPopoverController显示在屏幕上,UIPopoverController背后的所有控件默认是不能跟用户进行正常交互的
    点击UIPopoverController区域外的控件,UIPopoverController默认会消失
    要想点击UIPopoverController区域外的控件时不让UIPopoverController消失,解决办法是设置passthroughViews属性
    @property (nonatomic, copy) NSArray *passthroughViews;
    这个属性是设置当UIPopoverController显示出来时,哪些控件可以继续跟用户进行正常交互。这样的话,点击区域外的控件就不会让UIPopoverController消失了
    

    写在最后


    官网对UIPopoverPresentationController介绍
    Human Interface Guidelines——Popovers

    相关文章

      网友评论

        本文标题:UIPopoverPresentationController

        本文链接:https://www.haomeiwen.com/subject/hwvmhxtx.html