iOS 无法更改 UITableViewCell 中标签的 X 位置
iOS Unable to change X position of label in UITableViewCell
在左侧的标签下方和右侧的开关下方以编程方式出现在 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
}
您的代码试图添加多个相同的锚点约束。据推测,您会在调试控制台中收到一条 auto-layout 警告来解释这一点。
您需要创建一个可以修改的约束:
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
}()
// add these vars
var labelControlLeftAnchor: NSLayoutConstraint!
var switchControlRightAnchor: NSLayoutConstraint!
// 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
// create labelControlLeftAnchor
labelControlLeftAnchor = labelControl.leftAnchor.constraint(equalTo: leftAnchor, constant: 100)
labelControlLeftAnchor.isActive = true
addSubview(switchControl)
switchControl.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
//switchControl.rightAnchor.constraint(equalTo: rightAnchor, constant: -20).isActive = true
// create switchControlRightAnchor
switchControlRightAnchor = switchControl.rightAnchor.constraint(equalTo: rightAnchor, constant: -20)
switchControlRightAnchor.isActive = true
}
现在,您可以更改 .constant
值以“移动”对象:
// for example
labelControlLeftAnchor.constant = 40
switchControlRightAnchor.constant = -40
在左侧的标签下方和右侧的开关下方以编程方式出现在 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
}
您的代码试图添加多个相同的锚点约束。据推测,您会在调试控制台中收到一条 auto-layout 警告来解释这一点。
您需要创建一个可以修改的约束:
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
}()
// add these vars
var labelControlLeftAnchor: NSLayoutConstraint!
var switchControlRightAnchor: NSLayoutConstraint!
// 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
// create labelControlLeftAnchor
labelControlLeftAnchor = labelControl.leftAnchor.constraint(equalTo: leftAnchor, constant: 100)
labelControlLeftAnchor.isActive = true
addSubview(switchControl)
switchControl.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
//switchControl.rightAnchor.constraint(equalTo: rightAnchor, constant: -20).isActive = true
// create switchControlRightAnchor
switchControlRightAnchor = switchControl.rightAnchor.constraint(equalTo: rightAnchor, constant: -20)
switchControlRightAnchor.isActive = true
}
现在,您可以更改 .constant
值以“移动”对象:
// for example
labelControlLeftAnchor.constant = 40
switchControlRightAnchor.constant = -40