UIAlertController着色颜色默认为蓝色
我使用下面的代码来呈现一个UIAlertController操作表,其中的项目文本为红色。 我使用了tint属性来设置颜色。
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; alertController.view.tintColor = [UIColor redColor];
文本颜色似乎是默认为高亮或select蓝色。 这是正常的,我该怎么办?
这是一个已知的Bug,请参阅https://openradar.appspot.com/22209332
要修复它,重新应用完成处理程序中的色调颜色。 这里是我的Swift解决scheme,你将能够很容易地适应ObjC:
alertController.view.tintColor = UIColor.redColor() // apply 1st time to prevent flicker from Blue to Red when displaying navigationController?.presentViewController(alertController, animated: true, completion: { // Bugfix: iOS9 - Tint not fully Applied without Reapplying alertController.view.tintColor = UIColor.redColor() })
一个注意:这并不完全修复这个Bug。 您将注意到设备旋转时button重新着色系统默认(=蓝色)色调。
预计它会被修复与iOS 9.1。
编辑10/23/2015:仍然不固定与iOS 9.1。 几天前发布的iOS 9.1 + Xcode 7.1(7B91B)重新testing。 截至目前设置.tintColor不起作用,但作为评论,你可以设置整个应用程序的tintColor,例如在AppDelegate didFinishLaunchingWithOptions设置window?.tintColor = UIColor.redColor()
。 这也是AlertControllerbutton的色彩,但在某些情况下可能不适用,因为在整个应用程序中都会应用此色调。
在presentViewController后面添加tintColor。 适用于iOS 9.0.2
[self presentViewController:alertController animated:YES completion:nil]; [alertController.view setTintColor:[UIColor yellowColor]];
您也可以更改appdelegate中的应用颜色。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window.tintcolor = [UIColor yellowColor]; return YES; }
对我来说是完美的。
你可以像这样改变button的颜色
UIAlertAction* button = [UIAlertAction actionWithTitle:@"Delete Profile" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { //Add Action. }]; [button setValue:[UIColor redColor] forKey:@"titleTextColor"];
通过使用此行[buttonsetValue:[UIColor redColor] forKey:@“titleTextColor”]; 您可以更改您的操作表的button颜色
在traitCollectionDidChange
的子类中的traitCollectionDidChange
中设置色调颜色。
override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) { super.traitCollectionDidChange(previousTraitCollection) self.view.tintColor = UIColor.redColor() }
只要改变你的看法tintAdjustmentMode到UIViewTintAdjustmentModeNormal,所以它不会改变它在dimm上的颜色。
我仍然困惑你想达到什么目的。 但是你可以试试苹果公司创造Destructive
button的方式(默认文本颜色是红色的)。
您正在创buildUIAlertAction
的代码不使用您想要的红色button的Default
样式。 而是使用UIAlertActionStyleDestructive
。 示例代码:
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) { [view dismissViewControllerAnimated:YES completion:nil]; }];
UIAlertController适用于iOS 8及更高版本,因此旧版本的设备存在缺陷。 具有相应版本或更高版本的设备没有问题。
为了防止快速“popup”新的色调颜色,警报控制器的alpha值可以animation化。 然后它看起来完全一样,如果没有错误:
alertController.view.alpha = 0.0 presentViewController(alertController, animated: false) { alertController.view.tintColor = UIColor.redColor() UIView.animateWithDuration(0.2, animations: { alertController.view.alpha = 1.0 }, completion: nil) }
要像这样设置自定义颜色和字体子类UIAlertController
。
import UIKit class StyledAlertController: UIAlertController { private var cancelText:String? override func viewDidLoad() { super.viewDidLoad() view.tintColor = YourColor } override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() findLabel(view) } private func findLabel(scanView: UIView!) { if (scanView.subviews.count > 0) { for subview in scanView.subviews { if let label:UILabel = subview as? UILabel { if (cancelText != nil && label.text == cancelText!) { dispatch_async(dispatch_get_main_queue(),{ label.textColor = YourColor label.tintColor = YourColor }) } let font:UIFont = UIFont(name: YourFont, size: label.font!.pointSize)! label.font = font } findLabel(subview) } } } }
像这样使用(正常情况下)
let StyledAlertController = StyledAlertController(title: "My Title", message: "My Message", preferredStyle: .ActionSheet) let cancelAction:UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in print("Cancel Action Click") } actionSheetController.addAction(cancelAction) let anotherAction:UIAlertAction = UIAlertAction(title: "Another Action", style: .Default) { action -> Void in print("Another Action Click") } actionSheetController.addAction(anotherAction:UIAlertAction) presentViewController(actionSheetController, animated: true, completion: nil)