更改UIActionSheetbutton中的文本颜色

在我的项目中,我使用UIActionSheet来显示某种sortingtypes。 我想更改操作表button中显示的文本的颜色。 我们如何改变文字颜色?

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

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

这在iOS8中适用于我

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

如果您想要浏览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]; } } } 

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

iOS 8:你可以简单地用UIAlertActionStyleDestructive这样的样式初始化你的UIAlertAction:

 UIAlertAction *delete = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){ // your code in here }]; 

这会使button的文本默认为红色。

在这里输入图像说明

@jrc解决scheme工作,但是当你点击buttontitleLabel颜色会改变。 试试这个在所有状态下保持相同的颜色。

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

编辑

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

尝试使用appearance协议[ NOT WORKING ]

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

使用这个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. } 

UIAppearance可以处理这个问题:

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

比通过子视图要容易得多,但我还没有在iOS 6或更早版本上testing过这个解决scheme。

斯威夫特的解释3 。 改变所有的颜色。

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