美文网首页
iOS 12 UIAlertController 层级变动处理

iOS 12 UIAlertController 层级变动处理

作者: AngryChocolate | 来源:发表于2018-08-06 11:11 被阅读0次

最近将公司测试机更新到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

相关文章

网友评论

      本文标题:iOS 12 UIAlertController 层级变动处理

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