iOS 学习笔记 -- UIAlertController 自定义

在平常的开发中,经常会用到UIAlertController,用的时候系统自带的样式不符合我们的需求,需要我们自己定义UIAlertController 标题和内容的文本样式。在这里我们通过kvc的思想来实现。在这里补充一点:在使用中发现这个方法只适用于iOS 12的系统,其他系统具体看UIAlertController的层级结构。



    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"这是标题" message:@"这是内容。\n1、这是内容一。\n2、这是内容二。\n3、这是内容三。\n4、这是内容四。" preferredStyle:(UIAlertControllerStyleAlert)];
    
    UIView *subView1 = alert.view.subviews[0].subviews[0].subviews[0].subviews[0].subviews[0];
 
   
   //打印 找到所对应的内容
    NSLog(@"==========>%@",subView1.subviews);
    
    UILabel * tmpMessageLabel =subView1.subviews[2];
    
    //在这里设置内容为向左对齐
    [tmpMessageLabel setTextAlignment:NSTextAlignmentLeft];
    
    UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    
    [alert addAction:cancelAction];
    
    [alert addAction:okAction];
    
    [self presentViewController:alert animated:YES completion:nil];

打印的信息为:

在 subView5.subviews 中的内容
(
    ">",
    ">",
    ">",
    ">",
    ">"
)

在这里就找到了标题和内容所对应的label,为所需的内容进行设置样式。


仅个人见解,如有错误请见谅!

你可能感兴趣的:(iOS,iOS,自定义,标题改变对齐方式)