在UIActionSheet按钮中更改文本颜色

kir*_*iri 33 uiactionsheet ios

在我的项目中,我UIActionSheet用于显示某些排序类型.我想更改操作表按钮中显示的文本的颜色.我们怎样才能改变文字颜色?

jrc*_*jrc 73

iOS 8: UIActionSheet已弃用.UIAlertController尊重-[UIView tintColor],所以这是有效的:

alertController.view.tintColor = [UIColor redColor];
Run Code Online (Sandbox Code Playgroud)

更好的是,在应用程序委托中设置整个窗口的色调颜色:

self.window.tintColor = [UIColor redColor];
Run Code Online (Sandbox Code Playgroud)

iOS 7:在您的操作表委托中,实现-willPresentActionSheet:如下:

- (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];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 太糟糕了,这在iOS 8中不起作用. (15认同)
  • 这仅适用于默认状态.如果点击按钮,它会在titleLabel上保持蓝色. (3认同)
  • 谢谢.请尝试[button setTitleColor:fooColor forState:UIControlStateNormal]. (3认同)
  • 伙计们,这不是在使用私有API。-[UIView子视图]非常公开。但是,用这种方式更改文本颜色当然不是Apple想要的,因此您可以说它是“精神上”的私人工具。如果您的设计师或产品负责人要求这样做,这是一种方法。如果您不认为应该这样做,请不要这样做。故事结局。 (2认同)

lif*_*joy 19

在iOS 8中,这一切都不再适用.UIActionSheet文本似乎从window.tintColor获取其颜色.

  • 在iOS8中,不推荐使用UIActionSheet.您可以在警报控制器视图上使用UIAlertController并设置tintColor. (5认同)

bla*_*011 17

这适用于iOS8

[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)

  • 这将改变所有按钮的颜色,包括取消按钮.有没有办法将颜色更改为红色,但不是取消按钮? (4认同)

小智 12

如果您想浏览UIActionSheet子视图,则不应直接设置文本颜色,UILabel而应使用该UIButton setTitleColor:forState方法.否则,初始颜色将在UIControlEventTouchDragOutside等事件上重新设置.

这是正确的方法,重用jrc的代码:

- (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];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Jul*_*ius 6

可能是一个较晚的答案,但我认为它可以帮助某人。

iOS 8: 您可以简单地使用以下样式初始化UIAlertAction:UIAlertActionStyleDestructive,如下所示:

UIAlertAction *delete = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){

   // your code in here   

}];
Run Code Online (Sandbox Code Playgroud)

默认情况下,这会使按钮的文本变为红色。

在此处输入图片说明


小智 6

要更改警报操作中的特定按钮颜色,请使用此

let cancelAction = UIAlertAction(title: "Success", style: .cancel, handler: nil)
cancelAction.setValue(UIColor.green, forKey: "titleTextColor")

Run Code Online (Sandbox Code Playgroud)