How to change button text color of UIAlertView in iOS7?
Asked Answered
C

3

19

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?

Cindy answered 5/10, 2013 at 2:34 Comment(1)
Try to see PMAlertController a small library that allows you to substitute Apple's uncustomizable UIAlertController, with a beautiful and totally customizable alert.Bobine
E
26

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?

Elevator answered 5/10, 2013 at 13:39 Comment(6)
@downvoter instead of this cowardly downvote, please tell us if we to achieve that using UIAlertView!Elevator
-(void)willPresentAlertView:(UIAlertView *)alertView should give you an option to edit ALL subviews, and inevitably the cancel button is a subview of that 'alertview' object. couldn't find it though..Codee
You shouldn't. But you can.Remorseful
Just come across this and it is so correct so +1. As for the other two comments, @Codee just NO very very bad advise you Shouldn't mess with the view hierarchy of a UIAlertView this is perfectly clear in the documentation. Doing so is a one fast track ticket to getting your application rejected by Apple. as for WouterDS The only reason you can add subviews is because UIAlertView is a subclass of UIView which allows the view hierarchy to be messed with so whilst you can you SHouldn'tAlitaalitha
This is possible: [[UIView appearance] setTintColor:[UIColor redColor]]; However know that this tints all other tintable views too.Naturalism
This is no longer true. Kevin Renella's answer works great for me: [[UIView appearanceWhenContainedInInstancesOfClasses:@[[UIAlertView class]]] setTintColor:[UIColor redColor]];Analogue
I
2

UIActionSheet can also change buttons' color by this way.

[[UIView appearance] setTintColor:[UIColor redColor]];
Indolent answered 9/4, 2015 at 8:51 Comment(3)
What do you mean with "this way"?Cindy
[[UIView appearance] setTintColor:[UIColor redColor]]; can also change UIActionSheet buttons' color temporaryIndolent
@Indolent I copied "this way" from your comment to your answer.Elevator
L
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]];
Liverwurst answered 5/4, 2016 at 15:13 Comment(1)
This is the same exact answer that you posted https://mcmap.net/q/666765/-change-button-title-color-in-uialertview. You could have added a link to your previous question under the comments. Please don't post duplicate answersCheckup

© 2022 - 2024 — McMap. All rights reserved.