首页 技术 正文
技术 2022年11月13日
0 收藏 863 点赞 3,444 浏览 1672 个字

相信很多程序员都会遇到需求是这样的:

但是你发现无论怎么设置cancel和Destructive都无法让红色字体移动到下面取消按钮上:

其实之前一直用错,用了ios9之前的UIActionSheet这个view来当控件,但是发现,UIAlertController这个控制器用来做这个更好,只需一行代码就可以搞定。以下是我自己通过利用kvc去对字体颜色做改变的做法,希望有其他见解的可以随时交流。

– (void)viewDidLoad {

[super viewDidLoad];

self.button    = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 50)];

_button.backgroundColor  = [UIColor blueColor];

[_button setTitle:@”性别” forState:UIControlStateNormal];

[_button addTarget:self action:@selector(selectSheet) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:_button];

}

– (void)selectSheet

{

UIAlertAction *alertAct = [UIAlertAction actionWithTitle:@”男” style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

[_button setTitle:@”男” forState:UIControlStateNormal];

}];

UIAlertAction *alertAct1 = [UIAlertAction actionWithTitle:@”女” style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

[_button setTitle:@”女” forState:UIControlStateNormal];

}];

UIAlertAction *alertAct2 = [UIAlertAction actionWithTitle:@”保密” style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

[_button setTitle:@”保密” forState:UIControlStateNormal];

}];

UIAlertAction *alertAct3 = [UIAlertAction actionWithTitle:@”取消” style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

}];

//******通过利用kvc对字体颜色进行修改 *******//

[alertAct3 setValue:[UIColor redColor] forKey:@”_titleTextColor”];

UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

[alertControl addAction:alertAct];

[alertControl addAction:alertAct1];

[alertControl addAction:alertAct2];

[alertControl addAction:alertAct3];

[self presentViewController:alertControl animated:YES completion:nil];

}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297