UIPopoverPresentationController苹果官方文档介绍
https://developer.apple.com/documentation/uikit/uipopoverpresentationcontroller
UIPopoverPresentationController 对系统要求iOS8.0以上版本
首先咱们看下效果图:点击菜单->弹出菜单栏

//点击触发的方法:
- (IBAction)showMenuVC:(id)sender {
//1.创建弹出的视图控制器(UIViewController的子类)
MenuViewController * menuVC = [MenuViewController new];
//2.设置菜单视图控制器显示大小(UITableView的Size要和preferredContentSize相同)
menuVC.preferredContentSize = CGSizeMake(200, 200);
//3.设置视图控制器模态的类型
menuVC.modalPresentationStyle = UIModalPresentationPopover;
//4.设置弹出的位置
menuVC.popoverPresentationController.sourceView = sender;
//5.设置箭头的位置
menuVC.popoverPresentationController.sourceRect = [(UIButton *)sender bounds];
//6.设置弹出框的背景色(不同于视图控制器的视图背景色)
menuVC.popoverPresentationController.backgroundColor = [UIColor blackColor];
//7.设置箭头的方向
menuVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
/*
UIPopoverArrowDirectionUp 上,
UIPopoverArrowDirectionDown 下,
UIPopoverArrowDirectionLeft 左,
UIPopoverArrowDirectionRight 右,
UIPopoverArrowDirectionAny 系统判定
*/
//8.设置代理,要遵守UIPopoverPresentationControllerDelegate协议
menuVC.popoverPresentationController.delegate = self;
//9.显示菜单
[self presentViewController:menuVC animated:YES completion:nil];
}
//要实现的协议方法:
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
return UIModalPresentationNone; //不适配
}
//点击蒙版popover消失, 默认YES,所以该方法可以不用实现。
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
return YES;
}
以上就是实现带箭头的菜单栏的代码,是不是很简单!
网友评论