更改UIActionSheet中的项目颜色 – iOS 8

我一直在使用下面的代码来更改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]; } } } 

它在iOS7工作正常,但在iOS8上面的代码不会更改项目的文本颜色。

现在我的问题是如何改变UIActionSheet使用iOS8添加的项目的文本颜色?

附加信息:
我添加了断点来查看是否执行上面的代码中的以下行:

 UIButton *button = (UIButton *)subview; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 

这些行不会被执行。 所以, if([subview isKindOfClass:[UIButton class]])对于actionSheet.subviews所有项目总是为false 。 所以我认为UIActionSheet的观点已经改变了。

如果您仍想使用UIActionSheet而不是UIAlertController来支持较旧的iOS版本,那么有一个简单的方法。

UIActionSheet实际上在iOS 8中使用UIAlertController ,它有一个私有属性_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. } 

要更改button标题颜色,您需要使用UIAlertController并设置主viewtintColor

将button标题颜色设置为黑色的示例:

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

好吧,我遇到了同样的问题,但我想我find了一个解决scheme:

适当的方式应该是这样的,我猜想它适用于iOS7:

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

但是,由于ActionSheets“button”现在基于UICollectionView,所以它在iOS8中将不起作用。 所以经过一些挖掘,我得到了这个工作,而不是:

 [[UICollectionView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor blueColor]]; 

我正在使用它。

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

添加一行(AppDelegate)并适用于所有UIAlertController。

我有同样的任务,我今天做了这个黑客,肮脏,但它的作品

 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) } } } } 

改变windowtintColor为我工作。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method

写这个:

 [self.window setTintColor:[UIColor redColor]]; 

这改变了所有UIActionSheet对象的字体颜色。

对于iOS 7向后兼容性,并收集适当的默认色调颜色(苹果可能在未来的版本中会改变),我使用这个。 感谢关于默认色彩 的答案和卢卡斯的答案 。

  const Class alertControllerClass = NSClassFromString(@"UIAlertController"); if(alertControllerClass) { UIColor *defaultTintColour = UIView.new.tintColor; [[UIView appearanceWhenContainedIn: alertControllerClass, nil] setTintColor: defaultTintColour]; } 

注意虽然 – 你需要确保你使用这之前,你开始设置全局色调,否则UIView.new.tintColor只会给你你设置的当前色调。