最近將公司測試機更新到iOS 12 btea5版本,好提前作下適配。
其中遇到一個小問題,原本在iOS11中UIAlertController中Message文字屬性設(shè)置卻在iOS 12中出現(xiàn)了顯示問題。
這里先放上需求方案的 UIAlertController:

iOS normal.png
message內(nèi)容需要居左顯示。
再放上一張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中結(jié)果給了我點小驚喜
iOS 12 UIAlertController subviews.png

iOS 12 UIAlertController.png
圖層不難看出subviews[0]多出一個UIView,對它做一系列操作都沒看到啥效果(鬧呢)
一般這種需求,自定義的話會舒服很多