fileprivate let titleLabe:UILabel = {
let label = UILabel()
label.numberOfLines = 0
return label
}()
fileprivate let stackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.alignment = .leading
stackView.distribution = .fillProportionally
stackView.spacing = 8
return stackView
}()
fileprivate let verticalStackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .vertical
stackView.distribution = .equalSpacing
stackView.spacing = 8
return stackView
}()
verticalStackView.addConstraints(topAnchor: self.topAnchor,
leadingAnchor: self.leadingAnchor,
bottomAnchor: self.bottomAnchor,
trailingAnchor: self.trailingAnchor,
topConstant: 20,
leadingConstant: 16,
bottomConstant: 20,
trailingConstant: 16)
这样做,水平UIStackView
稍微向左对齐。但是,由于我要设置一个16点的leadingAnchor
常量,因此我希望它与titleLabel
如何解决此问题,以便stackView的titleLabel垂直对齐?
答案 0 :(得分:-1)
为UILabel
提供背景,以查看它实际上是从页边距开始还是文本的插图。
如果是这样,请尝试:
fileprivate let titleLabe:UILabel = {
let label = UILabel()
label.numberOfLines = 0
label.textAlignment = .left
return label
}()
无论如何,您说出问题的方式我不确定哪个元素应该与哪个元素对齐。请提供有关视图层次结构的更多详细信息。具有预期结果的图片也将有所帮助。