我遇到了问题,它不起作用。当我将点击分配给 StackView 元素并点击模拟器时,没有任何反应。请详细告诉我问题是什么,我的误解是什么。
我创建了包含 CustomCell 的 UITableView。 CustomCell 包含 ViewWrap,ViewWrap 包含以 View 作为元素的 StackView。我将 UITapGestureRecognizer 分配给包含 StackView 的视图。
附注将来,我将更改代码并为 StackView 中包含的所有视图创建两个用户类(点击会有不同的功能)。
class CustomCell: UITableViewCell {
static let ident = "cell"
let viewWrapp: UIView = {
let vwWrapp = UIView()
vwWrapp.translatesAutoresizingMaskIntoConstraints = false
return vwWrapp
}()
let stackView: UIStackView = {
var st = UIStackView()
st.translatesAutoresizingMaskIntoConstraints = false
st.axis = .horizontal
st.distribution = .fillEqually
st.alignment = .bottom
st.spacing = 0
st.isUserInteractionEnabled = true
return st
}()
var view11: UILabel = {
let vw = UILabel()
vw.translatesAutoresizingMaskIntoConstraints = false
vw.text = "4"
vw.font = UIFont(name: "LittleBird", size: 47)
vw.textAlignment = .center
vw.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: CustomCell.self, action: #selector(tabTab))
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1
vw.addGestureRecognizer(tapGesture)
return vw
}()
var view22: UILabel = {
let vw = UILabel()
vw.translatesAutoresizingMaskIntoConstraints = false
vw.text = "+"
vw.font = UIFont(name: "LittleBird", size: 30)
vw.textAlignment = .center
vw.layer.borderColor = CGColor(red: 1, green: 0, blue: 0, alpha: 1)
vw.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: CustomCell.self, action: #selector(tabTab))
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1
vw.addGestureRecognizer(tapGesture)
return vw
}()
// ... ...
// ... ...
// ... ...
var view77: UILabel = {
let vw = UILabel()
vw.translatesAutoresizingMaskIntoConstraints = false
vw.text = "4"
vw.font = UIFont(name: "LittleBird", size: 47)
vw.textAlignment = .center
vw.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: CustomCell.self, action: #selector(tabTab))
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1
vw.addGestureRecognizer(tapGesture)
return vw
}()
var view88: UILabel = {
let vw = UILabel()
vw.translatesAutoresizingMaskIntoConstraints = false
vw.text = "="
vw.font = UIFont(name: "LittleBird", size: 30)
vw.textAlignment = .center
return vw
}()
var view99: UILabel = {
let vw = UILabel()
vw.translatesAutoresizingMaskIntoConstraints = false
vw.textAlignment = .center
vw.font = UIFont(name: "LittleBird", size: 47)
vw.layoutIfNeeded()
vw.numberOfLines = 0
return vw
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
isUserInteractionEnabled = true
contentView.addSubview(viewWrapp)
stackView.addArrangedSubview(view11)
stackView.addArrangedSubview(view22)
stackView.addArrangedSubview(view33)
stackView.addArrangedSubview(view44)
stackView.addArrangedSubview(view55)
stackView.addArrangedSubview(view66)
stackView.addArrangedSubview(view77)
stackView.addArrangedSubview(view88)
stackView.addArrangedSubview(view99)
viewWrapp.addSubview(stackView)
setConstraints()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func awakeFromNib() {
super.awakeFromNib()
isUserInteractionEnabled = true
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
@objc func tabTab(){
print("Tab Tab")
}
func setConstraints() {
NSLayoutConstraint.activate([
viewWrapp.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
viewWrapp.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
viewWrapp.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
stackView.leadingAnchor.constraint(equalTo: viewWrapp.leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: viewWrapp.trailingAnchor),
stackView.heightAnchor.constraint(equalToConstant: 60)
])
}
}
我尝试将 UITapGestureRecognizer 分配给 StackView 中的视图,但点击没有响应。还添加了
isUserInteractionEnabled = true
于:
总结一下您的问题, 您正在尝试在视图中添加点击手势(在您的情况下是
UILabel
),但它不起作用。
有一个类似的问题,你可以检查如何在 UIStackview 上设置点击事件
要解决您的问题,众多解决方案之一就是使所有
UILabel
var
至 lazy var
除了解决方案之外, 您已将
translatesAutoresizingMaskIntoConstraints
添加到每个子项 UILabel
,这不是必需的,因为您仅在 NSLayoutConstraint
和 viewWrapp
中设置 stackView
。
我已经从您提供的代码片段中添加了一个可行的解决方案。
class CustomCell: UITableViewCell {
static let ident = "cell"
lazy var viewWrapp: UIView = {
let vwWrapp = UIView()
vwWrapp.translatesAutoresizingMaskIntoConstraints = false
return vwWrapp
}()
lazy var stackView: UIStackView = {
var st = UIStackView()
st.translatesAutoresizingMaskIntoConstraints = false
st.axis = .horizontal
st.distribution = .fillEqually
st.alignment = .bottom
st.spacing = 0
st.isUserInteractionEnabled = true
return st
}()
lazy var view11: UILabel = {
let vw = UILabel()
vw.text = "4"
vw.font = UIFont(name: "LittleBird", size: 47)
vw.textAlignment = .center
vw.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tabTab))
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1
vw.addGestureRecognizer(tapGesture)
return vw
}()
lazy var view22: UILabel = {
let vw = UILabel()
vw.text = "+"
vw.font = UIFont(name: "LittleBird", size: 30)
vw.textAlignment = .center
vw.layer.borderColor = CGColor(red: 1, green: 0, blue: 0, alpha: 1)
vw.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tabTab))
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1
vw.addGestureRecognizer(tapGesture)
return vw
}()
lazy var view77: UILabel = {
let vw = UILabel()
vw.text = "4"
vw.font = UIFont(name: "LittleBird", size: 47)
vw.textAlignment = .center
vw.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tabTab))
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1
vw.addGestureRecognizer(tapGesture)
return vw
}()
lazy var view88: UILabel = {
let vw = UILabel()
vw.text = "="
vw.font = UIFont(name: "LittleBird", size: 30)
vw.textAlignment = .center
return vw
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
stackView.addArrangedSubview(view11)
stackView.addArrangedSubview(view22)
stackView.addArrangedSubview(view77)
stackView.addArrangedSubview(view88)
contentView.addSubview(viewWrapp)
viewWrapp.addSubview(stackView)
setConstraints()
}
func setConstraints() {
NSLayoutConstraint.activate([
viewWrapp.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
viewWrapp.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
viewWrapp.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
stackView.topAnchor.constraint(equalTo: viewWrapp.topAnchor),
stackView.bottomAnchor.constraint(equalTo: viewWrapp.bottomAnchor),
stackView.leadingAnchor.constraint(equalTo: viewWrapp.leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: viewWrapp.trailingAnchor)
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func tabTab(){
print("Tab Tab")
}
}
除了我的解决方案之外,这个问题还可以通过多种方式解决。
例如:添加@HangarRash评论提到的按钮。