如何在iOS9中更改UIAlertController按钮文本颜色?

Ray*_*hen 25 objective-c ios uialertcontroller

问题类似于iOS 8 UIActivityViewController和UIAlertController按钮文本颜色使用窗口的tintColor但在iOS 9中.

我有一个UIAlertController,即使我试图设置,解除按钮也会保持白色

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

UIAlertController *strongController = [UIAlertController alertControllerWithTitle:title
                                                             message:message
                                                      preferredStyle:preferredStyle];
strongController.view.tintColor = [UIColor black];
Run Code Online (Sandbox Code Playgroud)

dba*_*art 56

我在过去遇到过类似的事情,这个问题似乎源于这样一个事实:警报控制器的视图还没有准备好在tintColor它出现之前接受更改.或者,尝试在显示警报控制器设置色调颜色:

[self presentViewController:strongController animated:YES completion:nil];
strongController.view.tintColor = [UIColor black];
Run Code Online (Sandbox Code Playgroud)

  • 但是,在轮换时,问题再次出现. (4认同)
  • 在显示警报控制器后为我设置色彩颜色.似乎违反直觉,绝不会猜到这一点.谢谢! (2认同)

Nei*_*ith 18

在Swift 3.x中:

我发现以下内容可以有效地运作.我在应用程序启动时调用它.

UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = UIColor.black
Run Code Online (Sandbox Code Playgroud)

因此,这将全局更改应用程序中所有UIAlertViewController按钮标签的色调颜色.它没有改变的唯一按钮标签颜色是那些具有破坏性UIAlertActionStyle的颜色.


MAh*_*ngh 13

Objective-C的

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title text"  message:@"Message text"  preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
//code here…
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Later" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
//code here….
}];
[ok setValue:[UIColor greenColor] forKey:@"titleTextColor"];
[cancel setValue:[UIColor redColor] forKey:@"titleTextColor"];
[alertController addAction:ok];
[alertController addAction:cancel];
[alertController.view setTintColor:[UIColor yellowColor]];
[self presentViewController:alertController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

斯威夫特3

let alertController = UIAlertController(title: "Title text", message: "Message text", preferredStyle: .alert)
let ok = UIAlertAction(title: "Yes" , style: .default) { (_ action) in
             //code here…
        }
let cancel = UIAlertAction(title: "Later" , style: .default) { (_ action) in
            //code here…
        }
ok.setValue(UIColor.green, forKey: "titleTextColor")
cancel.setValue(UIColor.red, forKey: "titleTextColor")
alertController.addAction(ok)
alertController.addAction(cancel)
alertController.view.tintColor = .yellow
self.present(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)


Mik*_*rne 12

我能够通过子类化来解决这个问题UIAlertController:

class MyUIAlertController: UIAlertController {

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        //set this to whatever color you like...
        self.view.tintColor = UIColor.blackColor()
    }
}
Run Code Online (Sandbox Code Playgroud)

当警报显示时,这会在设备旋转中存活.

使用此子类时,在显示警报后也不需要设置tintColor.

虽然在iOS 8.4中没有必要,但此代码也适用于iOS 8.4.

Objective-C实现应该是这样的:

@interface MyUIAlertController : UIAlertController
@end

@implementation MyUIAlertController
-(void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

    //set this to whatever color you like...
    self.view.tintColor = [UIColor blackColor];
}
@end
Run Code Online (Sandbox Code Playgroud)


小智 10

经过大量研究,我发现了如何使这项工作:

let cancelButton = UIAlertAction(title: button, style: UIAlertAction.Style.cancel, handler: { (action) in alert.dismiss(animated: true, completion: nil)
    })

    cancelButton.setValue(UIColor.systemBlue, forKey: "titleTextColor")
    alert.addAction(cancelButton)
Run Code Online (Sandbox Code Playgroud)

只需将 UIColor.systemBlue 更改为您想要的任何颜色,它就会使该按钮成为特殊颜色。我做了这个例子(我创建了 3 个 UIAlertActions 来实现它。):
在此处输入图片说明

仅使用 UIAlertAction.Style.whatever,它只能使其变为蓝色或红色。如果您更改 UIColor,它将使其成为您想要的任何颜色!


Dhe*_*j D 5

您可以使用以下方法更改它:Swift 3.x

    strongController.view.tintColor = UIColor.green
Run Code Online (Sandbox Code Playgroud)


Mik*_*e M 5

迅捷3

尝试使用,UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = MyColor但这可以防止其他与UIAlertControllertintColor配置无关的项目。我在尝试更改导航栏按钮项目的颜色时看到了它。

我改用了一个扩展程序(基于上面的Mike Taverne的回答),它的效果很好。

extension UIAlertController {

override open func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    //set this to whatever color you like...
    self.view.tintColor = MyColor
}
}
Run Code Online (Sandbox Code Playgroud)