水平UIStackView与垂直UIStackView中的UILabel不对齐

时间:2019-06-01 16:15:45

标签: ios swift uitableview uistackview

UILabel

fileprivate let titleLabe:UILabel = {
    let label = UILabel()
    label.numberOfLines = 0
    return label
}()

水平UIStackView

fileprivate let stackView: UIStackView = {
    let stackView = UIStackView()
    stackView.axis = .horizontal
    stackView.alignment = .leading
    stackView.distribution = .fillProportionally
    stackView.spacing = 8
    return stackView
}()

垂直UIStackView

fileprivate let verticalStackView: UIStackView = {
    let stackView = UIStackView()
    stackView.axis = .vertical
    stackView.distribution = .equalSpacing
    stackView.spacing = 8

    return stackView
}()

垂直UIStackView的约束

    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

对齐

enter image description here

如何解决此问题,以便stackView的titleLabel垂直对齐?

1 个答案:

答案 0 :(得分:-1)

UILabel提供背景,以查看它实际上是从页边距开始还是文本的插图。

如果是这样,请尝试:

fileprivate let titleLabe:UILabel = {
    let label = UILabel()
    label.numberOfLines = 0
    label.textAlignment = .left
    return label
}()

无论如何,您说出问题的方式我不确定哪个元素应该与哪个元素对齐。请提供有关视图层次结构的更多详细信息。具有预期结果的图片也将有所帮助。