Change text color in UIActionSheet buttons

2019-02-01 10:19发布

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?

10条回答
Explosion°爆炸
2楼-- · 2019-02-01 10:59

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];
        }
    }
}
查看更多
做个烂人
3楼-- · 2019-02-01 10:59

This works for me in iOS8

[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor redColor]];
查看更多
贼婆χ
4楼-- · 2019-02-01 11:00

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];
        }
    }
}
查看更多
别忘想泡老子
5楼-- · 2019-02-01 11:00

use this for ios 8 and above

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.
}
查看更多
贼婆χ
6楼-- · 2019-02-01 11:01

UIAppearance can handle this:

[[UIButton appearanceWhenContainedIn:[UIActionSheet class], nil] setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];

Much easier than going through the subviews, but I haven't tested this solution on iOS 6 or earlier.

查看更多
▲ chillily
7楼-- · 2019-02-01 11:03

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

查看更多
登录 后发表回答