iOS-UIAlertController设置按钮颜色

作者: FlyElephant | 来源:发表于2016-06-16 17:04 被阅读3010次

iOS8之后弹出框通过UIAlertController实现,但是有的时候需要设置UIAlertAction的文字颜色,我们可以先获取内部属性,通过键值对的形式进行设置:
<pre><code>unsigned int count=0; Ivar *ivars = class_copyIvarList([UIAlertAction class], &count); for (int i = 0; i<count; i++) { Ivar ivar = ivars[i]; NSLog(@"%s------%s", ivar_getName(ivar),ivar_getTypeEncoding(ivar)); }</code></pre>

FlyElephant.png

颜色设置

        NSString *title=NSLocalizedString(@"Objective-C", nil);
        NSString *tipContent=NSLocalizedString(@"FlyElephant", nil);
        UIAlertController *alertController=[UIAlertController alertControllerWithTitle:title message:tipContent preferredStyle:UIAlertControllerStyleAlert];
        UIColor *color=[UIColor redColor];
        UIAlertAction *sureAction=[UIAlertAction actionWithTitle:NSLocalizedString(@"确定", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        [sureAction setValue:color forKey:@"titleTextColor"];
        UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:NSLocalizedString(@"取消", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
        }];
        [cancelAction setValue:color forKey:@"titleTextColor"];
        [alertController addAction:sureAction];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];
FlyElephant.png

提示:iOS 8.3 之前没有此属性,如果需要支持iOS8.3以下需要判断以下是否存在该属性,否则会发生崩溃~

相关文章

  • iOS-UIAlertController设置按钮颜色

    iOS8之后弹出框通过UIAlertController实现,但是有的时候需要设置UIAlertAction的文字...

  • UISearchBar

    设置UISearchBar中 placeholder颜色 placeholder 输入字体颜色 背景颜色 清除按钮颜色

  • css样式学习-按钮

    利用css样式设置按钮 使用 background-color 属性来设置按钮颜色 使用 font-size 属性...

  • 更好用的Button(让你更好的使用按钮)

    这个按钮的好处 1·使用简单(即可以利用属性对按钮进行各种设置)2·可以支持设置按钮文字、按钮文字颜色、按钮文字大...

  • 02-统一设置导航栏文字颜色

    1、 统一设置设置返回按钮 2、设置状态栏颜色 3、 设置导航栏文字颜色 4、 设置导航栏标题的文字颜色 5、统一...

  • 导航栏UINavigationController

    2,设置导航栏的背景颜色 3,设置导航栏按钮字体颜色 4,设置标题样式与颜色(通过导航栏字典的方式) 5,设置返回...

  • swift UIButton用法详解

    创建 设置背景颜色 设置圆角边框 设置不同按钮状态显示 设置按钮状态不可用 设置文本字体 设置文本图标 修改图标文...

  • UISearchBar常用属性

    前言 记录下常用的属性,方便查阅 正文 设置取消按钮文本 设置取消按钮颜色 设置搜索框的背景,不是整个search...

  • iOS UITextfield属性

    设置UITextfield光标颜色 设置UITextfield文本向右偏移 自定义UITextfield清除按钮

  • Lottie 动画 官方用法 分析

    重新设置 所有按钮 的 显示效果 设置播放按钮的 颜色 (通过bool值来判断 当前按钮的 状态并为之赋予不同的颜...

网友评论

  • 33a02bf71691:为什么打印出来没有这个键呢titleTextColor,现在这个方法还适用么
    306a480fd470:如果是背景色 ,key值是什么呢
    33a02bf71691:@FlyElephant 哦
    FlyElephant:@重复昵称 iOS9中可以打印出来,iOS8没法打印~

本文标题:iOS-UIAlertController设置按钮颜色

本文链接:https://www.haomeiwen.com/subject/suimdttx.html