first I create a .xib file and named it RadioBtnView and connect it to a custom UIView class :
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 :
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
is this for creating the xib view programmatically or something else?
我终于知道原因了......我只是删除了radio.translatesAutoresizingMaskIntoConstraints = false
并添加了radio.frame = CGRect(x:50,y:100,width:120,height:20)
并且每一个思考工作似乎我们都可以不以编程方式添加 xib 视图
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.