简体   繁体   中英

UIButton Subclass Ignoring 'Touch Up Inside' Event

I have a subclass of a UIButton:

    import UIKit
    class CustomButton: UIButton {

        required init?(coder decoder: NSCoder) {
            super.init(coder: decoder)

            //Customizing the button appearance
        }
    }

In interface builder, in storyboards, under my main view controller, I dragged and dropped a stock button, and changed it's 'Custom Class' to CustomButton from the drop down menu.

Wired it's 'Touch Up Inside' event to an IBAction.

Custom code for appearance shows up, but the button is not firing the 'Touch Up Inside' .

When I checked for touch events (touch began, ended, eat) within the subclass, the button seems to be receiving touch.

Try disabling user interaction on the subviews that are part of your UIButton. This will allow the gestures to continue through to the button itself.

userInteractionEnabled = false

Found the issue:

When I customize the button, I added a UIVisualEffectView as its subview that covered the whole frame, and that canceled the any events the button was able to take. Quite unfortunate.

I had to go with a UIView subclass and add a tap gesture to mimic the touch events.

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