I\'m use the following code to present a UIAlertController action sheet with the item text as red. I\'ve used the tint property to set the color.
UIAlertCont
Set your tint color in traitCollectionDidChange
in a subclass of UIAlertController
.
override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
self.view.tintColor = UIColor.redColor()
}
I am still confused what you want to achieve. But you can try the Apple's way of creating Destructive
buttons(by default text color is red).
The code where you are creating UIAlertAction
s don't use the Default
style for the buttons you want in red color. Instead use UIAlertActionStyleDestructive
. Sample Code :
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action)
{
[view dismissViewControllerAnimated:YES completion:nil];
}];
To prevent the fast "popping up" of the new tint color the alpha value of the alert controller can be animated. Then it looks exactly the same as if there where no bug:
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)
}
Just add the tintColor after the presentViewController. Works on iOS 9.0.2
[self presentViewController:alertController animated:YES completion:nil];
[alertController.view setTintColor:[UIColor yellowColor]];
To set custom color and font subclass UIAlertController
like this.
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)
}
}
}
}
Use like this (as you normally would)
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)
You can also change the app tint color in appdelegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.tintcolor = [UIColor yellowColor];
return YES;
}
works perfect for me.