简体   繁体   中英

UIButton Touch Down not Registering in Swift

I have created a UIButton on the Interface Builder (Main.storyboard). I Connected two actions to the View Controller swift file, as you can see below:

@IBAction func backDownPress(sender: UIButton) {
    backButon.backgroundColor = UIColor.blackColor()
}
@IBAction func backNormalPress(sender: UIButton) {
    backButon.backgroundColor = UIColor.clearColor()
    performSegueWithIdentifier("pushFromRight", sender: self)
}

The top action is a Touch Down, and the bottom is a Touch up Inside.

However, once I try to run the app on either the Simulator or my iPhone, the touch doesn't register. When I try to just put a print line in the action, it also doesn't do anything.

Looking through the rest of my code, I can't find anything like disabling the user interaction, so I don't know why this doesn't work.

Well there are 2 possible solutions to this:

1) The @IBAction is not connected to the button.

2) There is something layered in front of the Button (a view of some kind)

I m guessing you want to change button color when pressed.

1. Subclass UIButton and implement

override var highlighted: Bool {
        get {
            return super.highlighted
        }
        set {
            if newValue {
                backgroundColor = UIColor.blueColor()
            }
            else {
                backgroundColor = UIColor.clearColor()
            }
            super.highlighted = newValue
        }
    }

Assuming your IBAction connections are proper: Check the userInteractionEnabled property for superview as subView's user interaction depends on superview as well. Check this SO link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM