Change tint color of UIAlertview and UIActionsheet buttons

19,686

Solution 1

As UIAlertView is deprecated You can. Use UIAlertController.

You can use tintColor property.

OLD

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.

-From Apple Doc

You can use tintColor property or You can use Some Custom Library for that, you can find it at cocoacontrols.com.

Solution 2

I was able to change the cancel button's text color to white in app delegate.

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

Solution 3

Combining best answers above, and updated for deprecation:

[[UIView appearanceWhenContainedInInstancesOfClasses:@[[UIAlertController class]]] setTintColor:[UIColor greenColor]];

or Swift:

UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .green

Works in 2018, Swift 4 / iOS 12.

Solution 4

You can adjust the color by searching and modifying the UILabel in the subview hierarchy of the alert window that is created right after showing the alert:

- (void)setButtonColor:(UIColor*)buttonColor {
    dispatch_after(dispatch_time(0,1), dispatch_get_main_queue(), ^{
        NSMutableArray *buttonTitles = [NSMutableArray array];
        for (NSUInteger index = 0; index < self.numberOfButtons; index++) {
            [buttonTitles addObject:[self buttonTitleAtIndex:index]];
        }
        for (UILabel *label in [[[UIApplication sharedApplication] keyWindow] recursiveSubviewsOfKind:UILabel.class]) {
            if ([buttonTitles containsObject:label.text]) {
                label.textColor = buttonColor;
                label.highlightedTextColor = buttonColor;
            }
        }
    });
}

[alert show];
[alert setButtonColor:UIColor.redColor];

The recursiveSubviewsOfKind: method is a category on UIView that returns an array of views in the complete subview hierarchy of the given class or subclass.

Solution 5

for UIAlertView with colored buttons you can use the cocoapod "SDCAlertView"

about CocoaPods: http://www.cocoapods.org

how to install CocoaPods: https://www.youtube.com/watch?v=9_FbAlq2g9o&index=20&list=LLSyp50_buFrhXC0bqL3nfiw

Share:
19,686
Bobrovsky
Author by

Bobrovsky

A smart and busy developer. Usually develop iOS and C#/ASP.NET projects. Here are some projects I'm currently involved in: Docotic.Pdf library An easy to use PDF library that can create PDFs from scratch, edit existing PDFs, extract text or images from PDF files, draw and print PDFs and many other things. Beautiful API, affordable prices, great support. LibTiff.Net library Free, open-source port of libtiff library written using C#. May be used in any application that needs to manipulate TIFF files. Natively supports Silverlight. Released under New BSD License.

Updated on July 21, 2022

Comments

  • Bobrovsky
    Bobrovsky almost 2 years

    I am trying to adapt my application for iOS 7. The issue I am having is I can not change the tint color of some controls.

    I did add

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    if (IOS7_OR_LATER)
        self.window.tintColor = [self greenTintColor];
    

    to my app delegate's

               - (BOOL)application:(UIApplication *)application
     didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    

    It mostly helped but color of message box and action sheet buttons is still the default blue.

    How can I recolor all such buttons too?

    Some screenshots:

    iOS7 message box iOS7 action sheet

  • Bobrovsky
    Bobrovsky almost 10 years
    Thank you. This works for UIAlertView but there are also UIBarButtonItems, for example. For UIBarButtonItem the effect is temporary: color reverts to default after tap or after pressing and holding the button.
  • Bobrovsky
    Bobrovsky almost 10 years
    Thank you. It looks promising.
  • Zeezer
    Zeezer over 9 years
    In iOS 8 there is alert.view.tintColor = UIColor.redColor()
  • ad1Dima
    ad1Dima over 8 years
    Saved my day! Must be in answer!
  • Kévin Renella
    Kévin Renella about 8 years
    Well, it does more than it should. I would rather use [[UIView appearanceWhenContainedIn:[UIAlertView class], nil] setTintColor:[UIColor whiteColor]]; and [[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor whiteColor]];
  • Kenn Cal
    Kenn Cal almost 8 years
    I'd rather @KévinRenella's answer as well. Ali's answer is very dangerous.
  • Tarek Hallak
    Tarek Hallak almost 8 years
    Answer has been copied from stackoverflow.com/a/19198435/1262634 ,at least change the text!
  • Dani
    Dani over 4 years
    @KévinRenella what would be the swift version of your improved answer?
  • Kévin Renella
    Kévin Renella over 4 years
    @DanielSpringer Even I am sure you could find this by yourself ;) UIView.appearance(whenContainedInInstancesOf: [UIAlertView.self]).tintColor = UIColor.white and UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = UIColor.white
  • Kévin Renella
    Kévin Renella over 4 years
    @DanielSpringer Now, the right answer using UIAlertController is to set the tintColor on the view of the controller. For example: alertController.view.tintColor = UIColor.white
  • Dani
    Dani over 4 years
    What I was missing was the .self in [UIAlertView.self]. I ended up figuring it out, but Xcode's errors weren't clear. Other than that, I did find it myself. 😉 Thanks!