iphone 与 ipad -- UIPopoverPresentationViewController
iOS8.0之后, 苹果推出了UIPopoverPresentationViewController, 在弹出控制器时, 统一采用 presentViewController,
但是要实现iPhone和iPad能够统一使用一段代码, 需要进行一些设置:
@implementation ViewController
// UIPopoverController只能用在iPad
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically fro
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
HMSecondViewController *vc = [[HMSecondViewController alloc] init];
// modal出来是个popover
vc.modalPresentationStyle = UIModalPresentationPopover;
// 取出vc所在的UIPopoverPresentationController
vc.popoverPresentationController.sourceView = self.slider;
vc.popoverPresentationController.sourceRect = self.slider.bounds;
[self presentViewController:vc animated:YES completion:nil];
}
@end
例如: UIAlertController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"你有严重的精神病,赶紧去治疗" preferredStyle:UIAlertControllerStyleActionSheet];
// 默认是POPover
alert.modalPresentationStyle = UIModalPresentationPopover;
// 设置popover指向的item
// alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;
alert.popoverPresentationController.sourceView = _clickBtn;
alert.popoverPresentationController.sourceRect = _clickBtn.bounds;
// 添加按钮
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"点击了确定按钮");
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"点击了取消按钮");
}]];
[self presentViewController:alert animated:YES completion:nil];
}