Change text color in UIActionSheet buttons

44,998

Solution 1

iOS 8: UIActionSheet is deprecated. UIAlertController respects -[UIView tintColor], so this works:

alertController.view.tintColor = [UIColor redColor];

Better yet, set the whole window's tint color in your application delegate:

self.window.tintColor = [UIColor redColor];

iOS 7: In your action sheet delegate, implement -willPresentActionSheet: as follows:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    for (UIView *subview in actionSheet.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
    }
}

Solution 2

In iOS 8, none of this works anymore. UIActionSheet text seems to get its color from window.tintColor.

Solution 3

This works for me in iOS8

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

Solution 4

If ever you want to go through the UIActionSheet's subviews, you should not directly set the text color of the UILabel but use the UIButton setTitleColor:forState method instead. Otherwise, the initial color will be set back upon events like UIControlEventTouchDragOutside for example.

Here is the proper way to do it, reusing the jrc's code:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    for (UIView *subview in actionSheet.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
    }
}

Solution 5

May be a late answer but I figure it could help someone.

iOS 8: You could simply initialize your UIAlertAction with the style: UIAlertActionStyleDestructive like so:

UIAlertAction *delete = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){

   // your code in here   

}];

This will make the button's text red by default.

enter image description here

Share:
44,998
kiri
Author by

kiri

Updated on October 10, 2020

Comments

  • kiri
    kiri over 3 years

    In my project, I am using UIActionSheet for displaying for some sorting type. I want to change the color of the text displayed in the action sheet buttons. How can we change the text color?

  • seufagner
    seufagner over 10 years
    This works only default state. If you tap the button, it keep blue color on the titleLabel.
  • jrc
    jrc over 10 years
    Thanks. Try [button setTitleColor:fooColor forState:UIControlStateNormal] instead.
  • Reuben Scratton
    Reuben Scratton over 10 years
    This just doesn't work at all on the iPhone simulator.
  • wuf810
    wuf810 about 10 years
    Reuben - Not sure what you're doing wrong but it works fine in Simulator.
  • lifjoy
    lifjoy almost 10 years
    Sorry for the late reply. My workaround was to stop changing window.tintColor or self.view.tintColor to white. Instead, I'm now explicitly setting any elements which need a white tintColor, rather than relying upon parent view tintColors.
  • Alex Cio
    Alex Cio over 9 years
    Nice post! I was wondering all the time why my UIAlertViews and UIAlertControllerappear in whiteColor.
  • Brody Robertson
    Brody Robertson over 9 years
    In iOS8, UIActionSheet is deprecated. You can use UIAlertController and set tintColor on the alert controller view.
  • msmialko
    msmialko over 9 years
    People, stop iterating through Apple's private controls subviews. This is bad, will crash one day and will make other developers laugh at you.
  • soulshined
    soulshined over 9 years
    @msmialko it's only private if it's not public. But it doesn't make it wrong, especially if Apple approves them. It's a shame Apple has to restrict access to simple fundamental features like the colors of a string just so we have to go and create our own from scratch. Have you ever tried to change the title color of the cancel button in a UISearchBar? I guess Apple thinks everyone sees only blue
  • kas-kad
    kas-kad over 9 years
    This is a guidelines violation, while setting your custom color to the destructive buttons (if one is presented in your sheet), which must be red.
  • Boda
    Boda over 9 years
    You are using something you assume Apple won't change, which is not the case in iOS 8, the code is not safe
  • jrc
    jrc over 9 years
    Guys, this isn't using a private API; -[UIView subviews] is very much public. But changing text color this way is of course not what Apple intended, so you could say it is private "in spirit". If your designer or product owner asks for this to be done, here is a way. If you don't think it should be done, don't do it. End of story.
  • Boda
    Boda over 9 years
    Yes it's not using private API but you are assuming the subviews inside the action sheet will remain UIButtons which is not valid, they might change by Apple cause they are not documented and not provided through public API
  • etayluz
    etayluz over 9 years
    This will change the color of all the buttons, including the cancel button. Is there a way to change the color to red, but not for the cancel button?
  • Alessandro Ornano
    Alessandro Ornano about 8 years
    @msmialko iterating using strong condition dont crash anything. I've olds app with ios 6 sdk actually running in ios9 devices..what do you understand is what do you doing..
  • Bijender Singh Shekhawat
    Bijender Singh Shekhawat about 6 years
    this will change all UIAlertController's colour which is used in your app.
  • Bijender Singh Shekhawat
    Bijender Singh Shekhawat about 6 years
    but +1 from my side.