【发布时间】:2013-04-23 07:44:52
【问题描述】:
在我的项目中,我使用UIActionSheet 来显示某些排序类型。我想更改操作表按钮中显示的文本的颜色。我们如何改变文本颜色?
【问题讨论】:
标签: ios uiactionsheet
在我的项目中,我使用UIActionSheet 来显示某些排序类型。我想更改操作表按钮中显示的文本的颜色。我们如何改变文本颜色?
【问题讨论】:
标签: ios uiactionsheet
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 获取颜色。
【讨论】:
UIAlertViews 和UIAlertController 出现在whiteColor 中。
这适用于我在 iOS8 中
[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor redColor]];
【讨论】:
如果你想通过UIActionSheet的子视图,你不应该直接设置UILabel的文本颜色,而是使用UIButtonsetTitleColor: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];
}
}
}
【讨论】:
要在警报操作中更改特定按钮的颜色,请使用此
let cancelAction = UIAlertAction(title: "Success", style: .cancel, handler: nil)
cancelAction.setValue(UIColor.green, forKey: "titleTextColor")
【讨论】:
@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];
}
}
}
【讨论】:
Swift 3 的解决方案。更改所有颜色。
UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .yellow
【讨论】:
编辑
这里有一个类似的问题:How to customize Buttons in UIActionSheet?
尝试使用appearance 协议[不工作]
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 或更早版本上测试过这个解决方案。
【讨论】: