UIButton在显示警报后更改背景颜色

时间:2019-02-06 18:28:36

标签: swift uibutton ios12

我在登录屏幕上使用的是圆形的UIButton,当用户单击登录按钮时,如果出现问题,我会执行登录过程,我会显示一个警报,但在显示警报后,按钮会更改其背景。

我从情节提要中设置了背景颜色。
我使用自定义的UIButton类将角和阴影添加到按钮上。

class RoundedShadowButton: UIButton {
var corners: CGFloat = 10
override var bounds: CGRect {
    didSet {
        setCorners()
        setupShadow()
    }
}

private func setupShadow() {
    self.layer.masksToBounds = true
    self.layer.cornerRadius = 6.0
    self.dropShadow(color: UIColor.gray, opacity: 0.4, offSet: CGSize(width: 1, height: 1), radius: 6, scale: true)
}

private func setCorners(){
    self.layer.cornerRadius = self.corners
    self.layer.masksToBounds = true
}
}


@IBAction func loginPressed(_ sender: AnyObject) {
        /*view.endEditing(true)
        if(txtMail.text == "" || txtPassword.text == ""){
            return
        }
        settings.setValue("normal", forKey: "loginType")
        settings.setValue(txtMail.text, forKey: "email")
        settings.setValue(txtPassword.text, forKey: "password")
        makeLogin()*/
        AlertManager(title: "Test", message: "Test").show()
    }


对于警报,我使用此类:

class AlertManager{
    let alert:UIAlertController
    var addAceptButton: Bool = true
    init(title:String, message:String) {
        self.alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
    }
    func addAction(action:UIAlertAction) -> Void {
        self.alert.addAction(action)
    }   
    func addActionAndRemoveFirst(action:UIAlertAction) -> Void {
        self.addAction(action: action)
        addAceptButton = false
    }
    func show() -> Void {
        DispatchQueue.main.async {
            if self.addAceptButton{
                self.alert.addAction(UIAlertAction(title: "Aceptar", style: UIAlertActionStyle.default, handler: nil))
            }
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            var cont = appDelegate.window!.rootViewController!
            while((cont.presentedViewController) != nil){
                cont = cont.presentedViewController!
            }
            cont.present(self.alert, animated: true, completion: nil)

        }
    }
}

Before the color change After the color change

0 个答案:

没有答案