0%

iOS下'Popover弹窗'的配置方法

Popover弹窗


获取UIPopoverPresentationControllerl

UIPopoverPresentationController类实例不需要直接创建,因为在UIViewController中有一个popoverPresentationController属性,可以从它获取。

1
2
3
4
UIViewController * controller = [[UIViewController alloc] init];
controller.preferredContentSize = CGSizeMake(100, 100);
controller.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController * popController = [controller popoverPresentationController];

设置代理与弹出源

出现在UIBarButtonItem上

1
2
popController.delegate = self;
popController.barButtonItem = self.barItem;

出现在UIView上

1
2
3
popController.delegate = self;
popController.sourceView = self.view;
popController.sourceRect = self.view.bounds;

实现代理方法

代理类实现协议

1
2
3
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}

弹窗显示、消失

显示(由源视图控制器)

1
[self presentViewController:controller animated:YES completion:nil];

消失(由弹窗控制器自身)

1
[self dismissViewControllerAnimated:YES completion:nil];