【发布时间】:2021-02-05 19:57:49
【问题描述】:
在左侧的标签下方和右侧的开关以编程方式出现在 TableViewCell 中。我可以使用以下方法控制开关的 X 位置:
switchControl.rightAnchor.constraint(equalTo: rightAnchor, constant: -20).isActive = true
但左侧标签的可比较指令什么也不做,它始终保持在默认的左侧位置。
labelControl.leftAnchor.constraint(equalTo: leftAnchor, constant: 100).isActive = true
我是否必须声明标签的宽度,而使用开关它是已知的?
lazy var labelControl: UILabel = {
let labelControl = UILabel()
labelControl.translatesAutoresizingMaskIntoConstraints = false
return labelControl
}()
lazy var switchControl: UISwitch = {
let switchControl = UISwitch()
switchControl.isOn = true
switchControl.onTintColor = UIColor.orange
switchControl.translatesAutoresizingMaskIntoConstraints = false
switchControl.addTarget(self, action: #selector(handleSwitchAction), for: .valueChanged)
return switchControl
}()
// MARK: - Init
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(labelControl)
labelControl.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
labelControl.leftAnchor.constraint(equalTo: leftAnchor, constant: 100).isActive = true
addSubview(switchControl)
switchControl.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
switchControl.rightAnchor.constraint(equalTo: rightAnchor, constant: -20).isActive = true
}
【问题讨论】:
标签: ios uitableview tableview