簡體   English   中英

創建自定義 xib 類的實例使手勢識別器不起作用

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

首先,我創建一個 .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]

    }
}

然后嘗試在 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)

但是 change(_:) 函數沒有響應似乎它沒有調用

這是為了以編程方式創建 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