更改 UIActionSheet 中项目的文本颜色 - iOS 8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24154889/
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 of Items in UIActionSheet - iOS 8
提问by Salman Zaidi
I had been using following code to change text color of items which I add in UIActionSheet
.:
我一直在使用以下代码来更改我添加的项目的文本颜色UIActionSheet
。:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
for (UIView *subview in actionSheet.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subview;
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
}
}
It works fine in iOS7
, but in iOS8
the above code doesn't change the text color of items.
它在 中工作正常iOS7
,但在iOS8
上面的代码中不会改变项目的文本颜色。
Now my question is how to change the text color of items which I add in UIActionSheet
using iOS8
??
Additional Info:
I added breakpoint to see whether following lines from the above code get executed or not:
现在的问题是如何改变我在新增项目的文本颜色UIActionSheet
使用iOS8
?
附加信息:
我添加了断点以查看上面代码中的以下几行是否被执行:
UIButton *button = (UIButton *)subview;
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
These lines do not get executed. So, if([subview isKindOfClass:[UIButton class]])
is always false
for all items of actionSheet.subviews
. So what I think is that the view heirarchy of
UIActionSheet
has been changed.
这些行不会被执行。因此,if([subview isKindOfClass:[UIButton class]])
始终false
适用于 的所有项目actionSheet.subviews
。所以我认为 的视图层次结构
UIActionSheet
已经改变。
回答by nonamelive
There's an easy way if you still want to use UIActionSheet
instead of UIAlertController
in order to support older iOS versions.
如果您仍想使用UIActionSheet
而不是UIAlertController
为了支持较旧的 iOS 版本,则有一种简单的方法。
UIActionSheet
actually uses UIAlertController
in iOS 8, and it has a private property _alertController
.
UIActionSheet
实际上UIAlertController
在 iOS 8 中使用,并且它有一个私有属性_alertController
。
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.
}
For Swift Below code should works
对于 Swift 下面的代码应该可以工作
let alertAction = UIAlertAction(title: "XXX", style: .default) { (action) in
}
alertAction.setValue(UIColor.red, forKey: "titleTextColor")
回答by DiscDev
To change the button title color you need to use UIAlertController
and set the tintColor
of the main view
.
要更改按钮标题颜色,您需要使用UIAlertController
并设置tintColor
main 的view
.
Example of setting the button title colors to black:
将按钮标题颜色设置为黑色的示例:
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"How would you like to proceed?" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *finish = [UIAlertAction actionWithTitle:@"Finish" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
[self performSelector:@selector(finish:) withObject:nil];
}];
UIAlertAction *playAgain = [UIAlertAction actionWithTitle:@"Play Again" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
[self performSelector:@selector(playAgain:) withObject:nil];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action)
{
[actionSheetController dismissViewControllerAnimated:YES completion:nil];
}];
[actionSheetController addAction:finish];
[actionSheetController addAction:playAgain];
[actionSheetController addAction:cancel];
//******** THIS IS THE IMPORTANT PART!!! ***********
actionSheetController.view.tintColor = [UIColor blackColor];
[self presentViewController:actionSheetController animated:YES completion:nil];
回答by Janne
Ok, I ran into the same problem but I think I have found a solution:
好的,我遇到了同样的问题,但我想我已经找到了解决方案:
The appropriate way should be like this and I guess it works in iOS7:
适当的方法应该是这样的,我想它在 iOS7 中有效:
[[UIButton appearanceWhenContainedIn:[UIActionSheet class], nil] setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
But it will not work in iOS8 due to the fact that the ActionSheets "buttons" is now based on a UICollectionView. So after some digging around I got this to work instead:
但由于 ActionSheets“按钮”现在基于 UICollectionView,它在 iOS8 中不起作用。因此,经过一番挖掘后,我改为使用它:
[[UICollectionView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor blueColor]];
回答by Lucas Torquato
I'm using it.
我正在使用它。
[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor blueColor]];
Add one line (AppDelegate) and works for all UIAlertController.
添加一行 (AppDelegate) 并适用于所有 UIAlertController。
回答by Igor
I have same task and I've made this hack today, dirty, but it works
我有同样的任务,我今天做了这个 hack,很脏,但它有效
class CustomAlertViewController: UIAlertController {
internal var cancelText: String?
private var font: UIFont? = UIFont(name: "MuseoSansCyrl-500", size: 12)
override func viewDidLoad() {
super.viewDidLoad()
self.view.tintColor = UIColor.blackColor()
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.findLabel(self.view) //if you need ios 9 only, this can be made in viewWillAppear
}
func findLabel(scanView: UIView!) {
if (scanView.subviews.count > 0) {
for subview in scanView.subviews {
if let label: UILabel = subview as? UILabel {
if (self.cancelText != nil && label.text == self.cancelText!) {
dispatch_async(dispatch_get_main_queue(),{
label.textColor = UIColor.redColor() //for ios 8.x
label.tintColor = UIColor.redColor() //for ios 9.x
})
}
if (self.font != nil) {
label.font = self.font
}
}
self.findLabel(subview)
}
}
}
}
回答by Benjohn
For iOS 7 backwards compatibility, and to collect the proper default tint colour (that Apple may well change in future versions), I use this. Thanks to the answers about default tintand to Lucas's answerabove.
为了向后兼容 iOS 7,并收集正确的默认色调颜色(Apple 可能会在未来版本中更改),我使用它。感谢上面关于默认色调的答案和卢卡斯的回答。
const Class alertControllerClass = NSClassFromString(@"UIAlertController");
if(alertControllerClass)
{
UIColor *defaultTintColour = UIView.new.tintColor;
[[UIView appearanceWhenContainedIn: alertControllerClass, nil] setTintColor: defaultTintColour];
}
Caution though – you need to ensure that you use this beforeyou start setting the global tint otherwise the UIView.new.tintColor
will just give you the current tint you have set up.
但是请注意 -在开始设置全局色调之前,您需要确保使用它,否则UIView.new.tintColor
只会为您提供您设置的当前色调。
回答by Abdullah Umer
Changing the tintColor
of window
worked for me.
改变tintColor
的window
工作对我来说。
In the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method
在里面 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method
write this:
写这个:
[self.window setTintColor:[UIColor redColor]];
This changed the font color of all UIActionSheet
objects.
这改变了所有UIActionSheet
对象的字体颜色。
回答by Lukas Mohs
Swift 4
斯威夫特 4
let alert = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
alert.view.tintColor = UIColor.black
self.present(alert, animated: true, completion: nil)
回答by Kuntal Gajjar
For Swift you can do like this
对于 Swift 你可以这样做
let alertAction = UIAlertAction(title: "XXX", style: .default) { (action) in
}
alertAction.setValue(UIColor.red, forKey: "titleTextColor")