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条回答
Evening l夕情丶
2楼-- · 2019-02-01 11:06

@jrc solution works, but titleLabel color changes when you tap the button. Try this to keep the same color at all states.

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{     
 UIColor *customTitleColor = [UIColor greenColor];
      for (UIView *subview in actionSheet.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
          UIButton *button = (UIButton *)subview;

          [button setTitleColor:customTitleColor forState:UIControlStateHighlighted];
          [button setTitleColor:customTitleColor forState:UIControlStateNormal];
          [button setTitleColor:customTitleColor forState:UIControlStateSelected];
        }
      }
}
查看更多
萌系小妹纸
3楼-- · 2019-02-01 11:10

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

查看更多
劳资没心,怎么记你
4楼-- · 2019-02-01 11:18

Soulution for Swift 3. Changes all colors.

UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .yellow
查看更多
萌系小妹纸
5楼-- · 2019-02-01 11:22

EDIT

Here is a similar question: How to customize Buttons in UIActionSheet?

Try to use the appearance protocol [NOT WORKING]

 UIButton *btn = [UIButton appearanceWhenContainedIn:[UIActionSheet class], nil];
 [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
查看更多
登录 后发表回答