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

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

相關閱讀更多精彩內容

友情鏈接更多精彩內容