简体   繁体   English

创建自定义 xib 类的实例使手势识别器不起作用

[英]create instance of custom xib class make gesture recognizer don't work

first I create a .xib file and named it RadioBtnView and connect it to a custom UIView class :首先,我创建一个 .xib 文件并将其命名为RadioBtnView并将其连接到自定义 UIView 类:

class CheckBtnView: UIView {
    @IBOutlet weak var mainView: UIView!
    @IBOutlet weak var CheckBtn: UIButton!
    @IBOutlet weak var checkDescription: UILabel!
    
    var status = false {
        didSet {
            if status {
                CheckBtn.backgroundColor = .orange
            } else {
                CheckBtn.backgroundColor = .clear
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupViews()
    }
    
    
    private func setupViews(){
        Bundle.main.loadNibNamed("CheckBtnView", owner: self, options: nil)
        addSubview(mainView)
        mainView.frame = self.bounds
        mainView.autoresizingMask = [.flexibleHeight,.flexibleWidth]

    }
}

then try to add an instance of RadioBtnView programmatically in viewController and add gesture recognizer to it :然后尝试在 viewController 中以编程方式添加 RadioBtnView 的实例并向其添加手势识别器:

let radio = RadioBtnView()
scrollContainerView.addSubview(radio)
radio.translatesAutoresizingMaskIntoConstraints = false
radio.topAnchor.constraint(equalTo: entry.bottomAnchor, constant: 15).isActive = true
radio.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 38).isActive = true
radio.isUserInteractionEnabled = true
let radioGesture = UITapGestureRecognizer(target: self, action: #selector(change(_:)))
radio.addGestureRecognizer(radioGesture)

but the change(_:) function doesn't respond seems like it didn't call但是 change(_:) 函数没有响应似乎它没有调用

is this for creating the xib view programmatically or something else?这是为了以编程方式创建 xib 视图还是其他什么?

我终于知道原因了......我只是删除了radio.translatesAutoresizingMaskIntoConstraints = false并添加了radio.frame = CGRect(x:50,y:100,width:120,height:20)并且每一个思考工作似乎我们都可以不以编程方式添加 xib 视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM