ios 更改 UIActionSheet 按钮中的文本颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16163737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change text color in UIActionSheet buttons
提问by kiri
In my project, I am using UIActionSheet
for displaying for some sorting type. I want to change the color of the text displayed in the action sheet buttons. How can we change the text color?
在我的项目中,我UIActionSheet
用于显示某种排序类型。我想更改操作表按钮中显示的文本颜色。我们如何更改文本颜色?
回答by jrc
iOS 8:UIActionSheet
is deprecated. UIAlertController
respects -[UIView tintColor]
, so this works:
iOS 8:UIActionSheet
已弃用。UIAlertController
尊重-[UIView tintColor]
,所以这有效:
alertController.view.tintColor = [UIColor redColor];
Better yet, set the whole window's tint color in your application delegate:
更好的是,在您的应用程序委托中设置整个窗口的色调:
self.window.tintColor = [UIColor redColor];
iOS 7:In your action sheet delegate, implement -willPresentActionSheet:
as follows:
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];
}
}
}
回答by lifjoy
In iOS 8, none of this works anymore. UIActionSheet text seems to get its color from window.tintColor.
在 iOS 8 中,这些都不再起作用了。UIActionSheet 文本似乎从 window.tintColor 获取其颜色。
回答by blacksheep_2011
This works for me in iOS8
这在 iOS8 中对我有用
[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor redColor]];
回答by Jonathan
If ever you want to go through the UIActionSheet
's subviews, you should not directly set the text color of the UILabel
but use the UIButton
setTitleColor:forState
method instead. Otherwise, the initial color will be set back upon events like UIControlEventTouchDragOutside for example.
如果您想查看UIActionSheet
的子视图,则不应直接设置 的文本颜色,UILabel
而应使用UIButton
setTitleColor:forState
方法。否则,初始颜色将在 UIControlEventTouchDragOutside 等事件时重新设置。
Here is the proper way to do it, reusing the jrc's code:
这是正确的方法,重用 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];
}
}
}
回答by Julius
May be a late answer but I figure it could help someone.
可能是一个迟到的答案,但我认为它可以帮助某人。
iOS 8:You could simply initialize your UIAlertAction with the style: UIAlertActionStyleDestructivelike so:
iOS 8:您可以简单地使用样式初始化 UIAlertAction:UIAlertActionStyleDestructive,如下所示:
UIAlertAction *delete = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
// your code in here
}];
This will make the button's text red by default.
这将使按钮的文本默认为红色。
回答by Asif
To change particular buttons colour in alert action use this
要更改警报操作中的特定按钮颜色,请使用此
let cancelAction = UIAlertAction(title: "Success", style: .cancel, handler: nil)
cancelAction.setValue(UIColor.green, forKey: "titleTextColor")
回答by kubacizek
Soulution for Swift 3. Changes all colors.
Soulution的斯威夫特3。改变所有颜色。
UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .yellow
回答by seufagner
@jrc solution works, but titleLabel color changes when you tap the button. Try this to keep the same color at all states.
@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];
}
}
}
回答by Ravish
use this for ios 8 and above
将此用于 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.
}
回答by alexcristea
EDIT
编辑
Here is a similar question: How to customize Buttons in UIActionSheet?
这是一个类似的问题:如何在 UIActionSheet 中自定义按钮?
Try to use the appearance
protocol [NOT WORKING]
尝试使用appearance
协议 [不工作]
UIButton *btn = [UIButton appearanceWhenContainedIn:[UIActionSheet class], nil];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];