iOS UIPopoverPresentationController 菜單視圖

最近項(xiàng)目中使用到pop形勢(shì)的一個(gè)菜單視圖,像微信、QQ右上角的點(diǎn)擊"+"展開(kāi)的一樣,就自己造了個(gè)輪子方便以后的使用。
?先上個(gè)圖
demo

圖一.png
圖二.png

首先想到的就是使用UIPopoverPresentationController來(lái)做,每個(gè)ViewController都有popoverPresentationController這樣的一個(gè)屬性,iOS 8開(kāi)始就有,正好項(xiàng)目也是從iOS 8開(kāi)始適配的,完美??,展開(kāi)方式有兩種方式:
?第一種:參照導(dǎo)航欄的barButtonItem

        // --設(shè)置過(guò)度樣式
        self.modalPresentationStyle = UIModalPresentationPopover;
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        // --顯示內(nèi)容的size大小
        self.preferredContentSize = CGSizeMake(width, CELL_HEIGHT * menuData.count);
        
        UIPopoverPresentationController *popController = [self popoverPresentationController];
        // --展開(kāi)時(shí)參照的barButtonItem
        popController.barButtonItem = barButtonItem;
        // --設(shè)置箭頭的方向
        popController.permittedArrowDirections = permittedArrowDirections;
        popController.delegate = self;

第一種:參照一般的view

        // --設(shè)置過(guò)度樣式
        self.modalPresentationStyle = UIModalPresentationPopover;
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        // --顯示內(nèi)容的size大小
        self.preferredContentSize = CGSizeMake(width, CELL_HEIGHT * menuData.count);
        
        UIPopoverPresentationController *popController = [self popoverPresentationController];
        // --展開(kāi)時(shí)參照的View
        popController.sourceView = view;
        popController.sourceRect = view.bounds;
        // --設(shè)置箭頭的方向
        popController.permittedArrowDirections = permittedArrowDirections;
        popController.delegate = self;

要實(shí)現(xiàn)下面的這個(gè)協(xié)議方法, 返回UIModalPresentationNone,不然會(huì)是全屏

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

接著調(diào)用presentViewController和dismissViewControllerAnimated就可以顯示和移除了,大功告成。
demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容