iOS UIPopoverPresentationController 菜单视图

最近项目中使用到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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容