How to change button text color of UIAlertView in iOS7?

夙愿已清 提交于 2019-12-17 18:34:49

问题


iOS7 introduced general tint-color. I think UIAlertView is also in the valid range, but actually tintColor doesn't look to work with UIAlertView. (for tappable button text color)

Is it possible to change the tint-color of alert view? If possible, how to change it?


回答1:


Unfortunately you cannot customize the appearance of alert views, it is impossible to change the buttons text color.

It is mentioned clearly in UIAlertView Class Reference:

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

Update:

The question was about iOS7, but now UIAlertView is deprecated, for UIAlertController you can simply change the view tint color:

    let alert = UIAlertController(title: "Gimme a break",
                                message: "For God sake its iOS7 question!",
                         preferredStyle: .alert)
    alert.view.tintColor = UIColor.red

It is helpful also to check this question: How to change UIAlertController button text colour in iOS9?




回答2:


The previous answers mentioning [[UIView appearance] setTintColor:[UIColor redColor]]; should work but this code is doing way more than if should if you want to change only the UIAlertView tint color.

You can aim the component this way on iOS < 9 :

[[UIView appearanceWhenContainedIn:[UIAlertView class], nil] setTintColor:[UIColor redColor]];
[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor redColor]];

And on iOS >9 :

[[UIView appearanceWhenContainedInInstancesOfClasses:@[[UIAlertView class]]] setTintColor:[UIColor redColor]];
[[UIView appearanceWhenContainedInInstancesOfClasses:@[[UIAlertController class]]] setTintColor:[UIColor redColor]];



回答3:


UIActionSheet can also change buttons' color by this way.

[[UIView appearance] setTintColor:[UIColor redColor]];


来源:https://stackoverflow.com/questions/19193367/how-to-change-button-text-color-of-uialertview-in-ios7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!