iOS 12 UIAlertController 层级变动处理

最近将公司测试机更新到iOS 12 btea5版本,好提前作下适配。

其中遇到一个小问题,原本在iOS11中UIAlertController中Message文字属性设置却在iOS 12中出现了显示问题。

这里先放上需求方案的 UIAlertController:

iOS normal.png

message内容需要居左显示。

再放上一张iOS 12 中的 UIAlertController:

iOS 12 error.png

再看看代码

        NSString *words = @"· You will earn Rs.100  Generic when your friend signs up on ******** by your referral links. \n· You will earn Rs.200 for every booking of your referred friend.";
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Share your \"phone book\" contacts" message:words preferredStyle:UIAlertControllerStyleAlert];
        UIView *subView1 = alertController.view.subviews[0];
        UIView *subView2 = subView1.subviews[0];
        UIView *subView3 = subView2.subviews[0];
        UIView *subView4 = subView3.subviews[0];
        UIView *subView5 = subView4.subviews[0];
        NSLog(@"%@",subView5.subviews);
        //取title和message:
        UILabel *title = subView5.subviews[0];
        UILabel *messageLabel = subView5.subviews[1];
        messageLabel.textAlignment = NSTextAlignmentLeft;
        NSMutableAttributedString *attributeText = [[NSMutableAttributedString alloc]initWithString:words];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineSpacing = 3;
        paragraphStyle.firstLineHeadIndent = 0;
        paragraphStyle.paragraphSpacing = 10;
        [attributeText addAttribute:NSParagraphStyleAttributeName
                              value:paragraphStyle
                              range:NSMakeRange(0, [words length] - 1)];
        messageLabel.attributedText = attributeText;
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];

因为项目是swift,无法打印subView5.subviews的包含视图,所以特地用oc写了一遍

iOS 11 UIAlertController subviews.png

iOS 11 UIAlertController.png

在iOS12之前UIAlertController的主体层次包含四个视图,反观iOS 12中结果给了我点小惊喜
iOS 12 UIAlertController subviews.png

iOS 12 UIAlertController.png

图层不难看出subviews[0]多出一个UIView,对它做一系列操作都没看到啥效果(闹呢)

一般这种需求,自定义的话会舒服很多

类似的文章

https://www.jianshu.com/p/b84e1bc81666

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容