UIAlertController change font color

后端 未结 8 781
时光取名叫无心
时光取名叫无心 2021-02-05 06:43

Here\'s my code that creates the UIAlertController

    // Create the alert controller
    var alertController = UIAlertController(title: \"Are you sure you want          


        
相关标签:
8条回答
  • 2021-02-05 07:37

    Swift 4.2

    One way of doing this is to make extension on UIAlertController, with this all of your app alerts whil have the same tint color. But it leaves destructive actions in red color.

     extension UIAlertController{
            open override func viewDidLayoutSubviews() {
                super.viewDidLayoutSubviews()
               self.view.tintColor = .yourcolor
            }
        }
    
    0 讨论(0)
  • 2021-02-05 07:39

    I have faced the same issue and spent a lot of time trying to find the best way to change it's color for iOS 9 and iOS 10 + because it's implemented in a different way.

    Finally I have made an extension for UIViewController. In extension I have added custom function which is almost equal to default function "present", but performs fix of colours. Here you are my solution. Applicable for swift 3+, for projects with target starting from iOS 9:

    extension UIViewController {
        /// Function for presenting AlertViewController with fixed colors for iOS 9
        func presentAlert(alert: UIAlertController, animated flag: Bool, completion: (() -> Swift.Void)? = nil){
            // Temporary change global colors
            UIView.appearance().tintColor = UIColor.red // Set here whatever color you want for text
            UIApplication.shared.keyWindow?.tintColor = UIColor.red // Set here whatever color you want for text
    
            //Present the controller
            self.present(alert, animated: flag, completion: {
                // Rollback change global colors
                UIView.appearance().tintColor = UIColor.black // Set here your default color for your application.
                UIApplication.shared.keyWindow?.tintColor = UIColor.black // Set here your default color for your application.
                if completion != nil {
                    completion!()
                }
            })
        }
    }
    

    To use this fixed function, you should just call this function instead of default present function. Example:

    self.presentAlert(alert: alert, animated: true)
    

    Same solution, but for UIActivityViewController:

    extension UIViewController {
        /// Function for presenting UIActivityViewController with fixed colors for iOS 9 and 10+
        func presentActivityVC(vc: UIActivityViewController, animated flag: Bool, completion: (() -> Swift.Void)? = nil) {
            // Temporary change global colors for changing "Cancel" button color for iOS 9 and 10+
            if UIDevice.current.systemVersion.range(of: "9.") != nil {
                UIApplication.shared.keyWindow?.tintColor = ColorThemes.alertViewButtonTextColor
            } else {
                UILabel.appearance().textColor = ColorThemes.alertViewButtonTextColor
            }
    
            self.present(vc, animated: flag) {
                // Rollback for changing global colors for changing "Cancel" button color for iOS 9 and 10+
                if UIDevice.current.systemVersion.range(of: "9.") != nil {
                    UIApplication.shared.keyWindow?.tintColor = ColorThemes.tintColor
                } else {
                    UILabel.appearance().textColor = ColorThemes.textColorNormal
                }
                if completion != nil {
                    completion!()
                }
            }
        }
    }
    

    I hope this will help somebody and will save a lot of time. Because my time was not saved by such detailed answer :)

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