iOS UIAlertController基本使用以及改变按钮颜色的方法

简单说一下UIAlertController的基本使用方法,废话不多说,直接上代码
1.基本的使用方法
//创建控制器
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"\n此用户不存在请去添加" preferredStyle:UIAlertControllerStyleAlert];
//创建添加按钮(可以写事件,也可以不写),可以创建多个按钮
UIAlertAction *OKbtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }];
//添加按钮到alertVC上,并模态出来
[alertVC addAction:OKbtn];
[self presentViewController:alertVC animated:YES completion:nil];

复制代码

UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"\n是否进行关联" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];

UIAlertAction *addBtn = [UIAlertAction actionWithTitle:@"关联" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
           }];

 [alertVC addAction:cancel];
 [alertVC addAction:addBtn];
 [self presentViewController:alertVC animated:YES completion:nil];

复制代码

添加文本框,因为文本框的大小调起来不太方便,所以这里有个小技巧,文本框的大小可以跟着字体的大小来变动

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入文件名称" preferredStyle:UIAlertControllerStyleAlert];

 [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
             //文本框占位文字       
            textField.placeholder = @"请输入文件名称";
            //改变字体大小
            [textField setFont:[UIFont systemFontOfSize:17]];
            //定义一个文本框来接收,传值用
            nameTextField = textField;
           }];
                
 UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    
                }];
UIAlertAction *addBtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

                 }];
[alert addAction:cancelBtn];
[alert addAction:addBtn];
[self presentViewController:alert animated:YES completion:nil];
复制代码

文本框比系统原来的要大一点,调大字体,文本框可以自适应,根据项目需求来吧

改变字体颜色
//创建控制器,不需要设置文字
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert];

//修改title
NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:@"提   示"];
//设置颜色
[alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, alertControllerStr.length)];
//设置大小
[alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, alertControllerStr.length)];
//替换title
[alertVC setValue:alertControllerStr forKey:@"attributedTitle"];

//修改message
NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:@"\n此用户不存在请去添加"];
//设置文字大小,并没有设置文字颜色
[alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, alertControllerMessageStr.length)];
//进行替换
[alertVC setValue:alertControllerMessageStr forKey:@"attributedMessage"];
//确定按钮
UIAlertAction *OKbtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }];
//添加
[alertVC addAction:OKbtn];
[self presentViewController:alertVC animated:YES completion:nil];

至于按钮的颜色,系统的style有红色可以选择,UIAlertActionStyleDestructive。

复制代码

到这里,UIAlertController的使用基本可以满足日常的开发需求了,如果有特殊需求,可以继续深入研究!!祝各位工作顺利!!!

转载于:https://juejin.im/post/5a311bc851882526151a98b5

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值