【问题标题】:Custom UIButton - IBAction not working自定义 UIButton - IBAction 不起作用
【发布时间】:2017-08-17 00:30:18
【问题描述】:

我有一个链接到 IBAction 的默认 UIButton(内部修饰)。这工作正常,所以我知道我的连接正常。一旦我将 UIButton 类更改为我的自定义按钮,IBAction 就不再被触发。下面是我的自定义 UIButton 的代码。任何想法为什么会发生这种情况?

import UIKit

@IBDesignable
class PrimaryButton: UIButton {

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

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


    private func initView() {
        let view = viewFromNibForClass()

        view.frame = bounds

        view.autoresizingMask = [
            UIViewAutoresizing.flexibleWidth,
            UIViewAutoresizing.flexibleHeight
        ]

        addSubview(view)
    }

    private func viewFromNibForClass() -> UIView {
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView

        return view
    }
}

【问题讨论】:

    标签: ios swift uibutton


    【解决方案1】:

    您的代码不起作用,因为按钮不再接收触摸。所有触摸都发送到您添加的最顶层自定义视图。

    设置view.userInteractionEnabled = false 使其对触摸透明。

    【讨论】:

      【解决方案2】:

      您的自定义按钮被您添加的视图所覆盖。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-14
        • 1970-01-01
        • 1970-01-01
        • 2021-03-17
        • 2016-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多