Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertController tint color defaults to blue on highlight

I'm using the following code to present a UIAlertController action sheet with the item text as red. I've used the tint property to set the color.

UIAlertController *alertController = [UIAlertController
                                      alertControllerWithTitle:nil
                                      message:nil
                                      preferredStyle:UIAlertControllerStyleActionSheet];
alertController.view.tintColor = [UIColor redColor];

The text color seems to be defaulting to blue on highlight or selection. Is this normal and how do I stop this?

like image 553
Bcf Ant Avatar asked Sep 21 '15 10:09

Bcf Ant


5 Answers

This is a known Bug, see https://openradar.appspot.com/22209332

To fix it, reapply the tint color in the Completion handler. Here's my Swift Solution, you will be able to adapt it easily for ObjC:

alertController.view.tintColor = UIColor.redColor() // apply 1st time to prevent flicker from Blue to Red when displaying

navigationController?.presentViewController(alertController, animated: true, completion: {
    // Bugfix: iOS9 - Tint not fully Applied without Reapplying
    alertController.view.tintColor = UIColor.redColor()
})

One Note: This does not fix the Bug entirely. You will notice upon Device Rotation that the Buttons are recolored with System Default (= Blue) tint.

Expect it to be fixed with iOS 9.1.

Edit 10/23/2015: Still not fixed with iOS 9.1. Retested with iOS 9.1 + Xcode 7.1 (7B91B) released a couple of days ago. As of now setting the .tintColor does not work, however as commented you can set the tintColor of the whole Application, e.g. in AppDelegate didFinishLaunchingWithOptions set window?.tintColor = UIColor.redColor(). This also tints the AlertController Buttons but may not be suitable in some cases as this tint is applied throughout the whole Application.

like image 160
Frederik Winkelsdorf Avatar answered Nov 17 '22 02:11

Frederik Winkelsdorf


Just add the tintColor after the presentViewController. Works on iOS 9.0.2

[self presentViewController:alertController animated:YES completion:nil];

[alertController.view setTintColor:[UIColor yellowColor]];
like image 30
Eddy Avatar answered Nov 17 '22 02:11

Eddy


You can also change the app tint color in appdelegate.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window.tintcolor = [UIColor yellowColor];
    return YES;
}

works perfect for me.

like image 32
marco v berlo Avatar answered Nov 17 '22 04:11

marco v berlo


Update for Swift 4, Xcode 9

private static func setupAppearanceForAlertController() {
    let view = UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self])
    view.tintColor = .black
}
like image 11
Ben M Avatar answered Nov 17 '22 02:11

Ben M


You can change button colour like this

UIAlertAction* button = [UIAlertAction
                              actionWithTitle:@"Delete Profile"
                              style:UIAlertActionStyleDefault
                              handler:^(UIAlertAction * action)
                              {
                                  //Add Action.
                              }];
[button setValue:[UIColor redColor] forKey:@"titleTextColor"];

By Using this line [button setValue:[UIColor redColor] forKey:@"titleTextColor"]; You can change the button colour of your action sheet

like image 5
Kaushik Movaliya Avatar answered Nov 17 '22 04:11

Kaushik Movaliya