preface:
1沧奴、支持協(xié)議
@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、代理方法
設(shè)置點擊蒙版是否消失
- (BOOL) popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
return YES;
}
默認返回的是覆蓋整個屏幕迫横,需設(shè)置成UIModalPresentationNone
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
return UIModalPresentationNone;
}
彈出視圖消失后調(diào)用的方法
- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
NSLog(@"dismissed");
}