preface:
1、支持协议
@interface ViewController ()<UIPopoverPresentationControllerDelegate>
2、添加按钮,并为其绑定方法
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.frame = CGRectMake(150, 100, 100, 100);
[_btn setTitle:@"PopOver" forState:UIControlStateNormal];
_btn.backgroundColor = [UIColor orangeColor];
[_btn addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:_btn];
3、pop方法
- (void)pop{
// 初始化弹出控制器
UIViewController *vc = [UIViewController new];
// 背景色
vc.view.backgroundColor = [UIColor yellowColor];
// 弹出视图的显示样式
vc.modalPresentationStyle = UIModalPresentationPopover;
// 1、弹出视图的大小
vc.preferredContentSize = CGSizeMake(300, 300);
// 弹出视图的代理
vc.popoverPresentationController.delegate = self;
// 弹出视图的参照视图、从哪弹出
vc.popoverPresentationController.sourceView = _btn;
// 弹出视图的尖头位置:参照视图底边中间位置
vc.popoverPresentationController.sourceRect = _btn.bounds;
// 弹出视图的箭头方向
vc.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
// 弹出
[self presentViewController:vc animated:YES completion:nil];
}
4、代理方法
设置点击蒙版是否消失
- (BOOL) popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
return YES;
}
默认返回的是覆盖整个屏幕,需设置成UIModalPresentationNone
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
return UIModalPresentationNone;
}
弹出视图消失后调用的方法
- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
NSLog(@"dismissed");
}
网友评论