最近項(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