我试图将nibs中任意数量的视图添加到UIStackView中。问题是,比方说,如果只包含一个视图,那么该视图将增长以填充整个stackview,而不是保持适当的宽度。这是我目前的代码:

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

关于ios - 如何以编程方式将 Nib 添加到UIStackView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51571405/

10-11 19:49