I had been using following code to change text color of items which I add in UIActionSheet
.:
- (void)willPresentActionSheet:(UIActionSheet *)act
There's an easy way if you still want to use UIActionSheet
instead of UIAlertController
in order to support older iOS versions.
UIActionSheet
actually uses UIAlertController
in iOS 8, and it has a private property _alertController
.
SEL selector = NSSelectorFromString(@"_alertController");
if ([actionSheet respondsToSelector:selector])
{
UIAlertController *alertController = [actionSheet valueForKey:@"_alertController"];
if ([alertController isKindOfClass:[UIAlertController class]])
{
alertController.view.tintColor = [UIColor blueColor];
}
}
else
{
// use other methods for iOS 7 or older.
}
For Swift Below code should works
let alertAction = UIAlertAction(title: "XXX", style: .default) { (action) in
}
alertAction.setValue(UIColor.red, forKey: "titleTextColor")
Changing the tintColor
of window
worked for me.
In the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method
write this:
[self.window setTintColor:[UIColor redColor]];
This changed the font color of all UIActionSheet
objects.
I'm using it.
[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor blueColor]];
Add one line (AppDelegate) and works for all UIAlertController.
For iOS 7 backwards compatibility, and to collect the proper default tint colour (that Apple may well change in future versions), I use this. Thanks to the answers about default tint and to Lucas's answer above.
const Class alertControllerClass = NSClassFromString(@"UIAlertController");
if(alertControllerClass)
{
UIColor *defaultTintColour = UIView.new.tintColor;
[[UIView appearanceWhenContainedIn: alertControllerClass, nil] setTintColor: defaultTintColour];
}
Caution though – you need to ensure that you use this before you start setting the global tint otherwise the UIView.new.tintColor
will just give you the current tint you have set up.
For Swift you can do like this
let alertAction = UIAlertAction(title: "XXX", style: .default) { (action) in
}
alertAction.setValue(UIColor.red, forKey: "titleTextColor")
To change the button title color you need to use UIAlertController
and set the tintColor
of the main view
.
Example of setting the button title colors to black:
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"How would you like to proceed?" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *finish = [UIAlertAction actionWithTitle:@"Finish" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
[self performSelector:@selector(finish:) withObject:nil];
}];
UIAlertAction *playAgain = [UIAlertAction actionWithTitle:@"Play Again" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
[self performSelector:@selector(playAgain:) withObject:nil];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action)
{
[actionSheetController dismissViewControllerAnimated:YES completion:nil];
}];
[actionSheetController addAction:finish];
[actionSheetController addAction:playAgain];
[actionSheetController addAction:cancel];
//******** THIS IS THE IMPORTANT PART!!! ***********
actionSheetController.view.tintColor = [UIColor blackColor];
[self presentViewController:actionSheetController animated:YES completion:nil];