UIButton changes background color after showing alert

I am using a rounded UIButton for my login screen, when the user click the login button I do my login process if something went wrong I show an alert but after showing it the button changes it background for its own.


I set te background color from the storyboard.

I use a custom UIButton class to add the corners and shadow to the button.

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()
  }

For the alert I use this class:

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)

  }
  }
}


Why it happens? Why the background color of the button changes after the alert present on the controller?

I read your post rapidly, but I suspect that

at the end of the alert you should reset the button hilite state to normal.

What do you mean with hilite state?

I mean the UIControl.State of the button


There is a property isHighlighted


Type Propertyhighlighted


Highlighted state of a control.


SDKs

  • iOS 2.0+
  • tvOS 9.0+
Framework

UIKit

On This Page
  • Declaration
  • Discussion



Declaration

static var highlighted: UIControl.State { get }

Discussion

A control becomes highlighted when a touch event enters the control’s bounds, and it loses that highlight when there is a touch-up event or when the touch event exits the control’s bounds. You can retrieve and set this value through the

isHighlighted
property.


Okey I understand but how can I reset the state of the button if is only get? I can't find a way to do it.

Well, use isHighlighted, it is get / set


var isHighlighted: Bool { get set }

Not working.

Test:


@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()*/
        let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
            switch action.style{
            case .default:
                self.btnLogin.isHighlighted = false
                
            case .cancel:
                self.btnLogin.isHighlighted = false
                
            case .destructive:
                self.btnLogin.isHighlighted = false
                
            }}))
        self.present(alert, animated: true, completion: nil)
    }
UIButton changes background color after showing alert
 
 
Q