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