How to change the background color of the UIAlertController?

前端 未结 10 1450
耶瑟儿~
耶瑟儿~ 2020-11-30 03:49

Due to strange behavior of UIActionSheet in iOS 8, I have implemented UIAlertController with UIAction as buttons in it. I would like to change the entire background of the U

10条回答
  •  一生所求
    2020-11-30 04:17

    func Alert(View: ViewController, Title: String, TitleColor: UIColor, Message: String, MessageColor: UIColor, BackgroundColor: UIColor, BorderColor: UIColor, ButtonColor: UIColor) {
    
        let TitleString = NSAttributedString(string: Title, attributes: [NSFontAttributeName : UIFont.systemFontOfSize(15), NSForegroundColorAttributeName : TitleColor])
        let MessageString = NSAttributedString(string: Message, attributes: [NSFontAttributeName : UIFont.systemFontOfSize(15), NSForegroundColorAttributeName : MessageColor])
    
        let alertController = UIAlertController(title: Title, message: Message, preferredStyle: .Alert)
    
        alertController.setValue(TitleString, forKey: "attributedTitle")
        alertController.setValue(MessageString, forKey: "attributedMessage")
    
        let okAction = UIAlertAction(title: "OK", style: .Default) { (action) in
    
        }
    
        let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
    
        alertController.addAction(okAction)
        alertController.addAction(cancelAction)
    
    
        let subview = alertController.view.subviews.first! as UIView
        let alertContentView = subview.subviews.first! as UIView
        alertContentView.backgroundColor = BackgroundColor
        alertContentView.layer.cornerRadius = 10
        alertContentView.alpha = 1
        alertContentView.layer.borderWidth = 1
        alertContentView.layer.borderColor = BorderColor.CGColor
    
    
        //alertContentView.tintColor = UIColor.whiteColor()
        alertController.view.tintColor = ButtonColor
    
        View.presentViewController(alertController, animated: true) {
            // ...
        }
    }
    

提交回复
热议问题