gpt4 book ai didi

ios - 如何以编程方式将 Nib 添加到 UIStackView

转载 作者:行者123 更新时间:2023-11-28 14:25:54 26 4
gpt4 key购买 nike

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

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 的 subview

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com