【问题标题】:Tap Gesture not working for custom view in swift点击手势不适用于swift中的自定义视图
【发布时间】:2022-07-06 01:21:36
【问题描述】:

我有一个 circleButton,向它添加手势识别器不起作用。 circleButton 是一个 UIView。

 @IBOutlet private weak var circleButton: UIView!

这是我的代码:

class CustomBottomBar: UIView {

@IBOutlet private weak var circleButton: UIView!
@IBOutlet private weak var bottomBarView: UIView!
@IBOutlet private weak var contentView: UIView!


override init(frame: CGRect) {
    super.init(frame: frame)
    commonInit()
}
   
override func layoutSubviews() {
    super.layoutSubviews()
    circleButton.layer.borderColor = UIColor.black.cgColor
    circleButton.layer.borderWidth = 2
}

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

@objc func onAddButtonClicked() {
    print("add")
}

private func commonInit() {
    if let customBar = Bundle.main.loadNibNamed("CustomBottomBar", owner: self, options: nil)?[0] as? UIView {
        customBar.frame = bounds
        addSubview(customBar)
        circleButton.layer.cornerRadius = circleButton.frame.height/2
        circleButton.clipsToBounds = true
        circleButton.isUserInteractionEnabled = true
        print(self.circleButton.frame.size)
        print(self.contentView.frame.size)
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onAddButtonClicked))
        circleButton.addGestureRecognizer(tapGesture)
    }
}

}

circleButton 的帧大小也正确。不确定是什么问题。我在故事板中使用 customBottomBar。

【问题讨论】:

    标签: ios swift uikit


    【解决方案1】:

    在这里,您首先添加条形并赋予它完整的边界

    customBar.frame = bounds
    addSubview(customBar)
    

    所以试着把按钮放在前面

    bringSubviewToFront(circleButton)
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onAddButtonClicked))
    circleButton.addGestureRecognizer(tapGesture)
    

    【讨论】:

    • 还是不行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多