UIAlertController tint color defaults to blue on highlight

后端 未结 11 1084
傲寒
傲寒 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: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)
    

提交回复
热议问题