在UIActionSheet按钮中更改文本颜色

时间:2013-04-23 07:44:53

标签: ios uiactionsheet

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

11 个答案:

答案 0 :(得分:73)

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

答案 1 :(得分:19)

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

答案 2 :(得分:17)

这适用于iOS8

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

答案 3 :(得分: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];
        }
    }
}

答案 4 :(得分:5)

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

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

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

   // your code in here   

}];

这将使按钮的文本默认为红色。

enter image description here

答案 5 :(得分: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];
        }
      }
}

答案 6 :(得分:1)

修改

以下是一个类似的问题:How to customize Buttons in UIActionSheet?

尝试使用appearance协议[ NOT WORKING ]

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

答案 7 :(得分: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.
}

答案 8 :(得分:1)

Swift 3 的洗脱。改变所有颜色。

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

答案 9 :(得分:0)

UIAppearance可以解决这个问题:

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

比浏览子视图容易得多,但我还没有在iOS 6或更早版本上测试过这个解决方案。

答案 10 :(得分:0)

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

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