美文网首页iOS开发
iPad 弹框奔溃

iPad 弹框奔溃

作者: 本帅不良 | 来源:发表于2020-04-02 11:26 被阅读0次

在使用系统弹框“UIAlertController”时,会莫名的崩溃。首先看下我们常规的写法:

UIAlertController *ac = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [ac addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //拍照
    }]];
    [ac addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //从相册选择
    }]];
    [ac addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:ac animated:YES completion:nil];
在 iPhone 下的运行效果如图: A291D9BD84595670DFCE9BC8E6B6AF1D.png

但是 iPad 屏幕比较宽,如果还是底部全屏显示的话会不会很丑?所以苹果不让你这样显示,需要设置一些参数。
改进版:

UIAlertController *ac = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [ac addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //拍照
    }]];
    [ac addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //从相册选择
    }]];
    [ac addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    if ([ac respondsToSelector:@selector(popoverPresentationController)]) {
        ac.popoverPresentationController.sourceView = cell; //必须加
        CGRect rect = cell.frame;
        ac.popoverPresentationController.sourceRect = CGRectMake(kScreenWidth-90, rect.origin.y-rect.size.height/2, rect.size.width, rect.size.height);//可选,我这里加这句代码是为了调整到合适的位置
    }
[self presentViewController:ac animated:YES completion:nil];

可以看到:我们只是加了一个 if 判断,并设置了 ac 的popoverPresentationController属性,我们可以看出这个属性是用来约束弹框显示的位置的。

iPad 下效果如图 D6C36C02AE95B38D2DF41ECE61D8F09F.png

注:这是弹框为“ UIAlertControllerStyleActionSheet”类型的显示情况,如果弹框为“UIAlertControllerStyleAlert”类型,由于都是居中显示,效果差异不大,故不需要这样设置。

相关文章

  • iPad 弹框奔溃

    在使用系统弹框“UIAlertController”时,会莫名的崩溃。首先看下我们常规的写法: 在 iPhone ...

  • iPad开发中的弹框接口UIPopoverController

    iPad特有的弹框接口 UIPopoverController用来实现弹框,有两种方式。UIPopoverCont...

  • UIMenuController 设置第一响应者时奔溃

    奇葩问题:设置第一响应者居然奔溃了。。。断点显示时这行奔溃了: 日志打印: 后来发现,原来页面中需要输入框,属性定...

  • xcode 中lldb命令调试

    调出lldb命令窗口分:程序发生奔溃,会在下面窗口中显示lldb输入框 设置断点是为了模拟奔溃,当然你也可以故意写...

  • DYLD, Library not loaded: /usr/l

    奔溃日志 奔溃表现:iOS12.1 及以下启动奔溃奔溃日志: 解决方法:关闭bitcode,重新打包上传appst...

  • iOS-千奇百怪的奔溃

    App 上线后,我们最怕的应该就是异常奔溃了。常见的奔溃类型分两种:信号可捕获奔溃、信号不可捕获奔溃,前者比较典型...

  • 奔溃

    送表弟们去上学的路上,听说刚才忘了拿《斗罗大陆》,却又接着说可以向同学借。我的内心立即奔溃了,满满的无力感! 当初...

  • 奔溃

    我总是想不让自己在乎别人的眼光,所以我总是在街上很自在,不担心自己的丑态被别人看到,我也经常对女友这么说,不要在意...

  • 奔溃

    工作到奔溃 加班到晚上八九点 没有人会理解你的苦楚,下班回家星空相伴,人与人之间的关系脆弱到分崩离析,工作后发现身...

  • 奔溃

    为什么当知道这个结果,我却奔溃了?原来不被期待的事情却偏偏来了,心里是那样的抵触。

网友评论

    本文标题:iPad 弹框奔溃

    本文链接:https://www.haomeiwen.com/subject/eswoyctx.html