在UIActionSheet按钮中更改文本颜色

38

在我的项目中,我使用UIActionSheet来显示一些排序类型。我想要更改操作表按钮中显示的文本颜色。我们该如何改变文本颜色?

12个回答

79

iOS 8: UIActionSheet已经被弃用。 UIAlertController会遵循-[UIView tintColor],所以以下代码可以使用:

alertController.view.tintColor = [UIColor redColor];

更好的办法是在您的应用程序代理中设置整个窗口的色调颜色:

self.window.tintColor = [UIColor redColor];

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];
        }
    }
}

4
这仅在默认状态下起作用。如果您点击按钮,它将使标题标签保持蓝色。 - seufagner
3
谢谢。尝试使用[button setTitleColor:fooColor forState:UIControlStateNormal]代替。 - jrc
1
你正在使用一些你认为苹果不会更改的东西,但在iOS 8中情况并非如此,代码是不安全的。 - user1453351
2
这并没有使用私有API; -[UIView subviews]非常公开。但是以这种方式更改文本颜色显然不是苹果想要的,所以你可以说它在“精神上”是私有的。如果您的设计师或产品所有者要求完成此操作,则可以使用以下方法。如果您认为不应该这样做,请不要这样做。故事结束。 - jrc
1
是的,它没有使用私有API,但您假设操作表内的子视图将保持为UIButtons,这是无效的,因为它们可能会被苹果更改,因为它们未记录并且未通过公共API提供。 - user1453351
显示剩余6条评论

20

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


抱歉回复晚了。我的解决方法是停止将window.tintColor或self.view.tintColor更改为白色。相反,我现在明确设置需要白色tintColor的任何元素,而不是依赖父视图的tintColors。 - lifjoy
很好的帖子!我一直在想为什么我的 UIAlertViewsUIAlertController 显示为 whiteColor - Alex Cio
5
在iOS8中,UIActionSheet已经被弃用。您可以使用UIAlertController,并在警告控制器视图上设置tintColor。 - Brody Robertson

17

这对我在iOS8上有效

[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor redColor]];

4
这将更改所有按钮的颜色,包括取消按钮。有没有一种方法可以将颜色改为红色,但不包括取消按钮? - etayluz

12

如果您想查看UIActionSheet的子视图,应该使用UIButtonsetTitleColor:forState方法而不是直接设置UILabel的文本颜色。否则,例如在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];
        }
    }
}

8

也许有点晚,但我认为它可以帮助到某些人。

iOS 8: 您可以使用样式UIAlertActionStyleDestructive来初始化UIAlertAction:

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

   // your code in here   

}];

这将使按钮的文本默认变为红色。 enter image description here

7

要更改警告操作中特定按钮的颜色,请使用以下方法

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


3

解决方案适用于 Swift 3。更改所有颜色。

UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .yellow

这将更改应用程序中使用的所有UIAlertController的颜色。 - Bijender Singh Shekhawat
但是我会额外加1分。 - Bijender Singh Shekhawat

2

@jrc的解决方案可以使用,但是当你点击按钮时titleLabel的颜色会发生改变。试试这个方法,使所有状态下的颜色保持一致。

- (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];
        }
      }
}

1

编辑

这里有一个类似的问题: 如何自定义UIActionSheet中的按钮?

尝试使用appearance协议[无效]

 UIButton *btn = [UIButton appearanceWhenContainedIn:[UIActionSheet class], nil];
 [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

1
使用此代码适用于iOS 8及以上版本。
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.
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接