繁体   English   中英

如何以编程方式将笔尖添加到UIStackView

[英]How to add nibs to a UIStackView programmatically

我正在尝试从笔尖添加任意数量的视图到UIStackView中。 问题是,如果只包含一个视图,则该视图会增长为填充整个堆栈视图,而不是保持适当的宽度。 到目前为止,这是我的代码:

friendStackView.autoresizingMask = .flexibleWidth
friendStackView.translatesAutoresizingMaskIntoConstraints = false

let friendView = Bundle.main.loadNibNamed("FriendView", owner: self, options: nil)?.first as! FriendViewController
friendStackView.addArrangedSubview(friendView)
friendView.heightAnchor.constraint(equalToConstant: 150).isActive = true
friendView.widthAnchor.constraint(equalToConstant: 120).isActive = true

它导致以下调试:

[LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x14eb9eb0 UIStackView:0x14ee6ed0.width == 500   (active)>",
    "<NSLayoutConstraint:0x14ef83f0 XXX.FriendViewController:0x14ef2090.width == 120   (active)>",
    "<NSLayoutConstraint:0x14def090 'UISV-canvas-connection' UIStackView:0x14ee6ed0.leading == XXX.FriendViewController:0x14ef2090.leading   (active)>",
    "<NSLayoutConstraint:0x14def230 'UISV-canvas-connection' H:[XXX.FriendViewController:0x14ef2090]-(0)-|   (active, names: '|':UIStackView:0x14ee6ed0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x14ef83f0 XXX.FriendViewController:0x14ef2090.width == 120   (active)>

您在这里有一个冲突,stackView width = 500,并且您正在尝试提供应该填充整个宽度120的子视图

"<NSLayoutConstraint:0x14eb9eb0 UIStackView:0x14ee6ed0.width == 500   (active)>",
"<NSLayoutConstraint:0x14ef83f0 XXX.FriendViewController:0x14ef2090.width == 120 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM