iPad-UIAlertController适配

最近项目中需要iPad适配,UIAlertController用的比较多,弹窗有两种模式:
<pre><code>typedef NS_ENUM(NSInteger, UIAlertControllerStyle) { UIAlertControllerStyleActionSheet = 0, UIAlertControllerStyleAlert } NS_ENUM_AVAILABLE_IOS(8_0);</code></pre>
当设置UIAlertControllerStyleAlert样式的时候iPad和iPhone都可以执行,当设置UIAlertControllerStyleActionSheet的时候,iPad就会崩溃,因此可以通过设置类型判断为iPad通过UIPopoverPresentationController弹出视图:
<pre><code>`

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"FlyElephant" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
__weak typeof(self) wself = self;
UIAlertAction *action = [UIAlertAction actionWithTitle:@"FlyElephant" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"取消", nil) style:UIAlertActionStyleCancel handler:NULL];
[alert addAction:action];
[alert addAction:cancelAction];

if (isIPad) {
    UIPopoverPresentationController *popPresenter = [alert popoverPresentationController];
    popPresenter.sourceView = self.button;
    popPresenter.sourceRect = self.button.bounds;
    
    [self presentViewController:alert animated:YES completion:nil];
}
else{
    [self presentViewController:alert animated:YES completion:NULL];
}`</code></pre>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,500评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,287评论 4 61
  • 去海边走走,沉醉于那辽阔的大海! 迎面扑来的海风让人很放松,海风真的可以瞬间把所有的烦恼都吹走。不得不敬畏大自然的...
    独木lin阅读 485评论 0 1
  • 难舍你的笑 笑纳你的别 别弃我的心 心妄我与你 ——《原狂录》
    不成气候的诗人阅读 157评论 0 1