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?
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];
}
}
}
In iOS 8, none of this works anymore. UIActionSheet text seems to get its color from window.tintColor.
This works for me in iOS8
[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor redColor]];
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];
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With