Swift自定义UIButton在单击时不工作

时间:2022-09-09 21:10:59

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.

我有一个用xib文件创建的自定义UIButton。当我在视图上使用我的自定义按钮时,当我按它时,它就不能工作了。

My RoundBtn.swift file:

我的RoundBtn。快速文件:

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:

我的RoundBtn。xib文件:

Swift自定义UIButton在单击时不工作

View where i used my custom button:

查看我使用自定义按钮的地方:

Swift自定义UIButton在单击时不工作

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).

我在所有视图组件上启用了userInteractionEnabled。当我点击我的自定义按钮时,它不起作用。我尝试通过编程方式定义click和定义action segue (show)。

@IBAction func myCartBtnPressed(sender: AnyObject) {
    print("my cart btn pressed")
}

2 个解决方案

#1


14  

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.

我认为这是由于你在当前的uiview按钮中添加了一些uiview子视图带有userInteractionEnabled,这意味着他们正在处理用户输入。

If you do:

如果你做的事:

view.isUserInteractionEnabled = false

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

RoundBtn本身会得到所有的触摸事件,而不是你上面的这个uiview。

#2


0  

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

指@fdiaz的回答。斯威夫特4符号。

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.

相反,如果您将一些UIButton(或用户交互视图)附加到一个UIImageView,您必须将它的isUserInteractionEnabled = true来让触摸通过层次结构向上到UIButton。

#1


14  

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.

我认为这是由于你在当前的uiview按钮中添加了一些uiview子视图带有userInteractionEnabled,这意味着他们正在处理用户输入。

If you do:

如果你做的事:

view.isUserInteractionEnabled = false

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

RoundBtn本身会得到所有的触摸事件,而不是你上面的这个uiview。

#2


0  

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

指@fdiaz的回答。斯威夫特4符号。

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.

相反,如果您将一些UIButton(或用户交互视图)附加到一个UIImageView,您必须将它的isUserInteractionEnabled = true来让触摸通过层次结构向上到UIButton。