Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change text color in UIActionSheet buttons

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?

like image 274
kiri Avatar asked Apr 23 '13 07:04

kiri


5 Answers

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];
        }
    }
}
like image 60
jrc Avatar answered Nov 03 '22 02:11

jrc


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

like image 30
lifjoy Avatar answered Nov 03 '22 02:11

lifjoy


This works for me in iOS8

[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor redColor]];
like image 17
blacksheep_2011 Avatar answered Nov 03 '22 01:11

blacksheep_2011


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];
        }
    }
}
like image 12
Jonathan Avatar answered Nov 03 '22 03:11

Jonathan


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

like image 8
Julius Avatar answered Nov 03 '22 01:11

Julius