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 中的内容
(
    "<UIView: 0x10fc01380; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0x282037d60>>",
    "<UILabel: 0x10fc01560; frame = (0 0; 0 0); text = '\U63d0\U73b0\U89c4\U5219'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x28033dc70>>",
    "<UILabel: 0x10fc01850; frame = (0 0; 0 0); text = '1\U3001\U63d0\U73b0\U91d1\U989d\U9700\U5728255.00\U5143\U4ee5\U4e0a\Uff0c\U4e0d\U8db3255.0...'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x28033ec10>>",
    "<UIView: 0x10fc01b40; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0x28202b0e0>>",
    "<UIView: 0x10fc01d20; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0x28202b120>>"
)

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


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值