如果在使用Auto Layout布局时显示了文本列中的尾部和前导警告,则可以按以下方式解决:
创建一个父级UIView,并添加约束。
在此UIView中创建两列文本。
为第一列文本添加以下约束:
a. 上部约束限制为父级UIView的上部。
b. 左侧约束限制为父级UIView的左侧。
c. 宽度约束限制为父级UIView的宽度的一半。
d. 底部约束限制为第二列文本的上部。
a. 上部约束限制为第一列文本的下部。
b. 右侧约束限制为父级UIView的右侧。
c. 宽度约束限制为父级UIView的宽度的一半。
d. 底部约束限制为父级UIView的底部。
这是一个示例代码:
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .lightGray
view.layer.cornerRadius = 5.0
self.view.addSubview(view)
NSLayoutConstraint.activate([
view.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 20),
view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 20),
view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -20),
view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -20)
])
let leftLabel = UILabel()
leftLabel.translatesAutoresizingMaskIntoConstraints = false
leftLabel.text = "Left Column"
leftLabel.numberOfLines = 0
leftLabel.backgroundColor = .white
leftLabel.layer.cornerRadius = 5.0
leftLabel.layer.borderColor = UIColor.black.cgColor
leftLabel.layer.borderWidth = 1.0
view.addSubview(leftLabel)
let rightLabel = UILabel()
rightLabel.translatesAutoresizingMaskIntoConstraints = false
rightLabel.text = "Right Column"
rightLabel.numberOfLines = 0
rightLabel.backgroundColor = .white
rightLabel.layer.cornerRadius = 5.0
rightLabel.layer.borderColor = UIColor.black.cgColor
rightLabel.layer.borderWidth = 1.0
view.addSubview(rightLabel)
NSLayoutConstraint.activate([
leftLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 10),
leftLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
leftLabel.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.5, constant: -15),
leftLabel.bottomAnchor.constraint(equalTo: rightLabel.topAnchor, constant: -10),
rightLabel.topAnchor.constraint