我们在使用UIAlertController的时候需要注意它在ipad上的使用。
UIAlertController有两种模式UIAlertControllerStyleAlert、UIAlertControllerStyleActionSheet,如果我们用的是UIAlertControllerStyleAlert模式,那么完全不用适配ipad,在iphone上和ipad上使用的效果是一样的,但如果我们用的是UIAlertControllerStyleActionSheet模式,那么这个时候我们需要适配ipad了。
同样的代码在iphone运行没问题,在ipad上运行却会崩溃。
适配ipad
在ipad上我们需要使用UIPopoverPresentationController
NS_CLASS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED @interface UIPopoverPresentationController : UIPresentationController
UIPopoverPresentationController iOS8开始使用的。
核心代码:
- (IBAction)sheetAction:(id)sender {
UIAlertController * alerC = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction * ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alerC addAction:ok];
UIButton * btn = (UIButton *)sender;
UIPopoverPresentationController *popPresenter = [alerC popoverPresentationController];
popPresenter.sourceView = btn;
popPresenter.sourceRect = btn.bounds;
[self presentViewController:alerC animated:YES completion:nil];
}
看下效果吧
iphone上的效果:

ipad上的效果

****本篇文章到这里就结束了,愿大家加班不多工资多,男同胞都有女朋友,女同胞都有男朋友。😊***
网友评论