UIAlertController tint color defaults to blue on highlight

后端 未结 11 1022
傲寒
傲寒 2021-01-01 10:56

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         


        
相关标签:
11条回答
  • 2021-01-01 11:02

    Set your tint color in traitCollectionDidChange in a subclass of UIAlertController.

    override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        self.view.tintColor = UIColor.redColor()
    }
    
    0 讨论(0)
  • 2021-01-01 11:04

    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 UIAlertActions 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];
    
                             }];
    
    0 讨论(0)
  • 2021-01-01 11:04

    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)
        }
    
    0 讨论(0)
  • 2021-01-01 11:05

    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]];
    
    0 讨论(0)
  • 2021-01-01 11:07

    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)
    
    0 讨论(0)
  • 2021-01-01 11:10

    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.

    0 讨论(0)
提交回复
热议问题