[英]Programmatically adding views to Vertical Stack View breaks constraints of vertical stack view
So I have a vertical stack view in a simple layout. 所以我有一个简单布局的垂直堆栈视图。 When I add a view through Interface Builder I see the view in the stack view no issues.
通过Interface Builder添加视图时,在堆栈视图中看不到该视图。
When I then add views programmatically to the UIStackView it breaks the constraints and the added views appear at the top of the window. 然后,当我以编程方式将视图添加到UIStackView时,它打破了约束,并且添加的视图显示在窗口顶部。 I'm confused as to why it would break the constraints of the stack view.
我对为什么它会破坏堆栈视图的约束感到困惑。
func buildCats(theJson:JSON){
//self.verticalStack.subviews.forEach({ $0.removeFromSuperview() })
print(theJson)
if let infos = self.swiftyJsonvar["info"].array{
for info in infos{
guard let v = UINib(nibName: "ticketOrderView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as? UIView else { return }
v.translatesAutoresizingMaskIntoConstraints = false
self.verticalStack.addArrangedSubview(v)
}
}
}
The added subviews need constraints, as they have no intrinsic size. 添加的子视图没有内部大小,因此需要约束。 Try: 尝试:
v.heightAnchor.constraint(greaterThanOrEqualToConstant: 30).isActive = true v.widthAnchor.constraint(greaterThanOrEqualToConstant: 200).isActive = true v.heightAnchor.constraint(greaterThanOrEqualToConstant:30).isActive = true v.widthAnchor.constraint(greaterThanOrEqualToConstant:200).isActive = true
Hmmm.... I'm doubting this now... I suspect the issue may be related to adding the views on a background thread. 嗯...。我现在对此表示怀疑...我怀疑问题可能与在后台线程上添加视图有关。 Maybe run the loop on the main thread? 也许在主线程上运行循环?
Edit 2: Ah... the problem I'm betting now is related to what you're doing with the view / subviews inside your "ticketOrderView" 编辑2:啊...我现在打赌的问题与您在“ ticketOrderView”中的视图/子视图所做的事情有关
For an example, see: https://github.com/DonMag/ScratchPad 有关示例,请参见: https : //github.com/DonMag/ScratchPad
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.