8

I have a custom UIButton created with xib file. When i use my custom button on my view, it does not work when i press to it.

My RoundBtn.swift file:

import UIKit

@IBDesignable class RoundBtn: UIButton {

    var nibName = "RoundBtn"

    @IBOutlet weak var btnImageView: UIImageView!
    @IBOutlet weak var btnLabel: UILabel!

    @IBInspectable var image: UIImage? {
        get {
            return btnImageView.image
        } set(image) {
            btnImageView.image = image
        }
    }

    @IBInspectable var label: String? {
        get {
            return btnLabel.text
        } set(label) {
            btnLabel.text = label
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    func setup() {
        let view = loadViewFromNib()
        view.frame = self.bounds
        view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
        btnImageView.layer.cornerRadius = 60/2
        btnImageView.layer.borderColor = UIColor(red: 5/255,
            green: 66/255, blue: 38/255, alpha: 1).CGColor
        btnImageView.layer.borderWidth = 2
        btnLabel.font = UIFont.boldSystemFontOfSize(14.0)
        btnImageView.userInteractionEnabled = true
        btnLabel.userInteractionEnabled = true
        view.userInteractionEnabled = true
        addSubview(view)
    }

    func loadViewFromNib() -> UIButton {
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: nibName, bundle: bundle)
        let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIButton   
        return view
    }   
}

My RoundBtn.xib file:

enter image description here

View where i used my custom button:

enter image description here

I enabled userInteractionEnabled on all view components. When i click to my custom button, it does not work. I tried by defining on click programmatically and by defining action segue (show).

@IBAction func myCartBtnPressed(sender: AnyObject) {
    print("my cart btn pressed")
}
kakajan
  • 2,614
  • 2
  • 22
  • 39

2 Answers2

27

I think this is caused because you're adding a couple of UIViews subviews to your current UIButton with userInteractionEnabled, which means that they're handling the user input.

If you do:

view.isUserInteractionEnabled = false

The RoundBtn itself will get all the touch events, instead of this UIViews that you have on top.

fdiaz
  • 2,600
  • 21
  • 27
  • Hi, at the beginning i don't enabled interactions programmatically and on click does not work anyway, then i google it, and found solutions for enabling interactions, i enabled all of them, anyway on click does not work – kakajan Feb 10 '16 at 14:52
  • now i tried by deleting that 3 lines, but also does not work, then i tried your solution, it worked :D , but please explain why i must disable on view when i need interaction? – kakajan Feb 10 '16 at 14:53
  • When you're adding a UIView to your RoundBtn, that UIView has userInteractionEnabled as true by default (as opposed to UIImageView and UILabel which have userInteractionEnabled as false by default). So all the touch events go to that view instead of going to your RoundBtn. I'd recommend taking a look at [Hit-Testing Returns the View Where a Touch Occurred](https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/event_delivery_responder_chain/event_delivery_responder_chain.html#//apple_ref/doc/uid/TP40009541-CH4-SW4) to get a better understanding. – fdiaz Feb 10 '16 at 15:44
  • 1
    saved my day, that was the solution! Thank you! – MkaysWork Apr 21 '16 at 11:59
  • 1
    Had this exact same problem in Swift 5 with XCode 11.2.1. To fix this problem, disable all user interaction in your storyboard in the "Attributes Inspector" tab (5th tab) and disable the user interaction enabled trait in the "Identity Inspector" tab (4th tab). – DevNebulae Nov 26 '19 at 15:12
0

Referring to @fdiaz's answer. Swift 4 notation.

Conversely, if you attach some UIButton (or user interactive view) to an UIImageView, you must turn its isUserInteractionEnabled = true to let the touches go through the hierarchy up down to the UIButton.

Stéphane de Luca
  • 12,745
  • 9
  • 57
  • 95