美文网首页
iOS UIPopoverPresentationControl

iOS UIPopoverPresentationControl

作者: 我是帅帅唐僧 | 来源:发表于2017-09-29 15:13 被阅读54次

    最近项目中使用到pop形势的一个菜单视图,像微信、QQ右上角的点击"+"展开的一样,就自己造了个轮子方便以后的使用。
     先上个图
    demo

    图一.png 图二.png

    首先想到的就是使用UIPopoverPresentationController来做,每个ViewController都有popoverPresentationController这样的一个属性,iOS 8开始就有,正好项目也是从iOS 8开始适配的,完美🍺,展开方式有两种方式:
     第一种:参照导航栏的barButtonItem

            // --设置过度样式
            self.modalPresentationStyle = UIModalPresentationPopover;
            self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
            // --显示内容的size大小
            self.preferredContentSize = CGSizeMake(width, CELL_HEIGHT * menuData.count);
            
            UIPopoverPresentationController *popController = [self popoverPresentationController];
            // --展开时参照的barButtonItem
            popController.barButtonItem = barButtonItem;
            // --设置箭头的方向
            popController.permittedArrowDirections = permittedArrowDirections;
            popController.delegate = self;
    

    第一种:参照一般的view

            // --设置过度样式
            self.modalPresentationStyle = UIModalPresentationPopover;
            self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
            // --显示内容的size大小
            self.preferredContentSize = CGSizeMake(width, CELL_HEIGHT * menuData.count);
            
            UIPopoverPresentationController *popController = [self popoverPresentationController];
            // --展开时参照的View
            popController.sourceView = view;
            popController.sourceRect = view.bounds;
            // --设置箭头的方向
            popController.permittedArrowDirections = permittedArrowDirections;
            popController.delegate = self;
    

    要实现下面的这个协议方法, 返回UIModalPresentationNone,不然会是全屏

    - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
        return UIModalPresentationNone;
    }
    

    接着调用presentViewController和dismissViewControllerAnimated就可以显示和移除了,大功告成。
    demo

    相关文章

      网友评论

          本文标题:iOS UIPopoverPresentationControl

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