iOS 12 UIAlertController 層級變動處理

最近將公司測試機更新到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,對它做一系列操作都沒看到啥效果(鬧呢)

一般這種需求,自定義的話會舒服很多

類似的文章

http://www.itdecent.cn/p/b84e1bc81666

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容