使用UIStackView的布局锚点时出错

时间:2017-06-30 15:41:48

标签: objective-c swift

尝试添加stackView和一些子代,但是会出现布局错误。我做错了什么?

当trailinAchors常数低于leadingAchors常数时,似乎会出现问题。

unite

错误:

    self.stackview.axis = .vertical
    self.stackview.alignment = .fill
    self.stackview.distribution = .fill
    self.stackview.spacing = 5
    self.addSubview(self.stackview)
    self.stackview.translatesAutoresizingMaskIntoConstraints = false
    self.stackview.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
    self.stackview.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true
    self.stackview.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0).isActive = true
    self.stackview.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0).isActive = true

    // Holder
    let holderView = UIView()
    holderView.backgroundColor = UIColor.red
    self.stackview.addArrangedSubview(holderView)

    // Subview
    let view3 = UIView()
    view3.backgroundColor = UIColor.orange
    holderView.addSubview(view3)

    view3.translatesAutoresizingMaskIntoConstraints = false
    view3.topAnchor.constraint(equalTo: holderView.topAnchor, constant: 0).isActive = true
    view3.bottomAnchor.constraint(equalTo: holderView.bottomAnchor, constant: 0).isActive = true
    view3.leadingAnchor.constraint(equalTo: holderView.leadingAnchor, constant: 30).isActive = true
    view3.trailingAnchor.constraint(equalTo: holderView.trailingAnchor, constant: -30).isActive = true

2 个答案:

答案 0 :(得分:0)

这是什么观点?我怀疑tableview的superview的前导和尾随锚点可能具有相同的值(相同的位置)。确保tableview的superview被适当地约束到它自己的superview(可能是视图控制器),因此框架的宽度和高度> gt; 0

答案 1 :(得分:0)

意识到我打电话了

layoutIfNeeded()

在函数的底部。当我删除它时,错误就消失了。