改变系统自带alertView的样式

本文介绍如何使用KVC方法自定义UIAlertController的样式,包括按钮颜色的设置。通过示例代码展示了如何创建UIAlertController并添加不同样式的按钮,同时提醒开发者关于按钮样式的限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先,我变动的是系统的UIAlertController,而UIAlertView也有对应的方法能改变样式,这里就不列举了。

方法比较简单,就不深入介绍了,主要运用的是KVC方法。

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *confirmButton = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[cancelButton setValue:k333333Color forKey:@"titleTextColor"];
[alertController addAction:cancelButton];
[alertController addAction:confirmButton];
[self presentViewController:alertController animated:YES completion:nil];

就可以设置对应的按钮颜色。

值得一提的是,这个alertController只允许设置一个样式为cancel的按钮,会自动排布在最左边。当你设置多于1个的时候,会报错。这一点千万记得。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值