iOS-UIAlertController设置按钮颜色

iOS8之后弹出框通过UIAlertController实现,但是有的时候需要设置UIAlertAction的文字颜色,我们可以先获取内部属性,通过键值对的形式进行设置:

    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));
    }
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 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,303评论 4 61
  • 我温柔的理由 是丁香摇落 芳魂缱绻 是温风送唇 轻吻指尖 是归鸟清啭 小扣耳畔 是心湖微澜 你在岸边
    无核苹果不伤芯阅读 233评论 0 0
  • 第三章 劫后风波频 刘凮临跃出窗外,也不辨方向,径直从二楼飞檐上纵身蹿入翠月轩后侧竹林。 夜风如潮,幽竹摇晃,传出...
    不正经先生L阅读 219评论 0 3