为什么嵌入式UIStackView(按比例填充)具有间距和布局边距,会导致约束错误以及如何解决? [英] Why do embedded UIStackView's, filled Proportionally, with spacing and Layout Margins causes constraint errors and how to fix?
问题描述
这个问题来自伟大的问答环节:
日志错误为:
2021-03-16 10:01:22.815997 + 1100 stackviewsWithProportionalFill [8528:4745644] Sherlock初始化了2021-03-16 10:01:23.051125 + 1100 stackviewsWithProportionalFill [8528:4745644] [LayoutConstraints]无法同时进行满足约束.以下列表中的约束中至少有一个是您不想要的约束.尝试以下操作:(1)查看每个约束,并尝试找出不期望的约束;(2)查找添加了一个或多个不必要约束的代码并进行修复.(<< NSLayoutConstraint:0x600003317e80'UISV-canvas-connection'UIStackView:0x7feb0270f930.leading == stackviewsWithProportionalFill.FixedLabel:0x7feb02715f80.Leading(active)>",< NSLayoutConstraint:0x600003317930'UISV-canvas-connection'H:[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0270b710]-(0)-|(活动,名称:"|":UIStackView:0x7feb0270f930)>,< NSLayoutConstraint:0x6000033172a0'UISV-按比例填充'stackviewsWithProportionalFill.ExpandingLabel:0x7feb0270b710.width == UIStackView:0x7feb0270f930.width(active)" ;,< NSLayoutConstraint:0x6000033178e0'UISV-spacing'H:[stackviewsWithProportionalFill.FixedLabel:0x7feb02715f80]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0270b710](处于活动状态)>")将尝试通过打破约束< NSLayoutConstraint:0x6000033178e0'UISV-spacing'H:[stackviewsWithProportionalFill.FixedLabel:0x7feb02715f80]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0270b710](处于活动状态)>在UIViewAlertForUnsatisfiableConstraints上创建一个符号断点,以在调试器中捕获该断点.< UIKitCore/UIView.h>中列出的UIView的UIConstraintBasedLayoutDebugging类别中的方法.可能也有帮助.2021-03-16 10:01:23.057974 + 1100 stackviewsWithProportionalFill [8528:4745644] [LayoutConstraints]无法同时满足约束.以下列表中的约束中至少有一个是您不想要的约束.尝试以下操作:(1)查看每个约束,并尝试找出不期望的约束;(2)查找添加了一个或多个不必要约束的代码并进行修复.(<< NSLayoutConstraint:0x60000332cb40 UIScrollView:0x7feb0381ea00.width == UIView:0x7feb0270aeb0.width(active)>,<< NSLayoutConstraint:0x60000332d270 UIStackView:0x7feb02709160.width == UIScrollView:0x7feb0381ea00.width(active)>< NSLayoutConstraint:0x600003370fa0'UISV-alignment'UILabel:0x7feb0270b2a0.leading == UIStackView:0x7feb02711930.Leading(active)>",<< NSLayoutConstraint:0x6000033713b0'UISV-alignment'UILabel:0x7feb0270b2a0.trailing == UIStackView:0x7feb02711930.trailing(active)>,< NSLayoutConstraint:0x600003310eb0'UISV-canvas-connection'UILayoutGuide:0x6000029208c0'UIViewLayoutMarginsGuide'.leading == stackviewsWithProportionalFill.FixedLabel:0x7feb02717140.leading(active)>,"< NSLayoutConstraint:0x6000033123f0'UISV-canvas-connection'UILayoutGuide:0x6000029208c0'UIViewLayoutMarginsGuide'.trailing == stackviewsWithProportionalFill.ExpandingLabel:0x7feb02717530.trailing(active)>,"< NSLayoutConstraint:0x600003370c80'UISV-canvas-connection'UIStackView:0x7feb02709160.leading == UILabel:0x7feb0270b2a0.Leading(active)>",< NSLayoutConstraint:0x600003370cd0'UISV-canvas-connection'H:[UILabel:0x7feb0270b2a0]-(0)-|(活动,名称:"|":UIStackView:0x7feb02709160)>",< NSLayoutConstraint:0x600003312530'UISV-spacing'H:[stackviewsWithProportionalFill.FixedLabel:0x7feb02717140]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb02717530](活动)>,"<< NSLayoutConstraint:0x600003371540'UIView-Encapsulated-Layout-Width'UIView:0x7feb0270aeb0.width == 0(active)>",< NSLayoutConstraint:0x600003312300'UIView-leftMargin-guide-constraint'H:|-(101)-[UILayoutGuide:0x6000029208c0'UIViewLayoutMarginsGuide'](LTR)(有效,名称:"|":UIStackView:0x7feb02711930)>",< NSLayoutConstraint:0x6000033123a0'UIView-rightMargin-guide-constraint'H:[UILayoutGuide:0x6000029208c0'UIViewLayoutMarginsGuide']-(0)-|(LTR)(活动,名称:'|':UIStackView:0x7feb02711930)>")将尝试通过打破约束< NSLayoutConstraint:0x600003312530'UISV-spacing'H:[stackviewsWithProportionalFill.FixedLabel:0x7feb02717140]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb02717530](活动)进行恢复在UIViewAlertForUnsatisfiableConstraints上创建一个符号断点,以在调试器中捕获该断点.< UIKitCore/UIView.h>中列出的UIView的UIConstraintBasedLayoutDebugging类别中的方法.可能也有帮助.2021-03-16 10:01:23.065566 + 1100 stackviewsWithProportionalFill [8528:4745644] [LayoutConstraints]无法同时满足约束.以下列表中的约束中至少有一个是您不想要的约束.尝试以下操作:(1)查看每个约束,并尝试找出不期望的约束;(2)查找添加了一个或多个不必要约束的代码并进行修复.(<< NSLayoutConstraint:0x60000332cb40 UIScrollView:0x7feb0381ea00.width == UIView:0x7feb0270aeb0.width(active)>,<< NSLayoutConstraint:0x60000332d270 UIStackView:0x7feb02709160.width == UIScrollView:0x7feb0381ea00.width(active)><< NSLayoutConstraint:0x6000033710e0'UISV-alignment'UILabel:0x7feb0270b2a0.leading == UIStackView:0x7feb02712730.Leading(active)>,<< NSLayoutConstraint:0x6000033714f0'UISV-alignment'UILabel:0x7feb0270b2a0.trailing == UIStackView:0x7feb02712730.trailing(active)>,< NSLayoutConstraint:0x6000033138e0'UISV-canvas-connection'UILayoutGuide:0x600002920a80'UIViewLayoutMarginsGuide'.leading == stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260e660.Leading(活动)>,"< NSLayoutConstraint:0x600003313930'UISV-canvas-connection'UILayoutGuide:0x600002920a80'UIViewLayoutMarginsGuide'.trailing == stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260ecd0.trailing(active)>,"< NSLayoutConstraint:0x600003370c80'UISV-canvas-connection'UIStackView:0x7feb02709160.leading == UILabel:0x7feb0270b2a0.Leading(active)>",< NSLayoutConstraint:0x600003370cd0'UISV-canvas-connection'H:[UILabel:0x7feb0270b2a0]-(0)-|(活动,名称:"|":UIStackView:0x7feb02709160)>",< NSLayoutConstraint:0x600003370050'UISV-spacing'H:[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260e660]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260ecd0](处于活动状态)>" ;,<< NSLayoutConstraint:0x600003371540'UIView-Encapsulated-Layout-Width'UIView:0x7feb0270aeb0.width == 0(active)>",< NSLayoutConstraint:0x6000033137f0'UIView-leftMargin-guide-constraint'H:|-(16)-[UILayoutGuide:0x600002920a80'UIViewLayoutMarginsGuide'](LTR)(有效,名称:"|":UIStackView:0x7feb02712730)>",< NSLayoutConstraint:0x600003313890'UIView-rightMargin-guide-constraint'H:[UILayoutGuide:0x600002920a80'UIViewLayoutMarginsGuide']-(17)-|(LTR)(活动,名称:"|":UIStackView:0x7feb02712730)>")将尝试通过打破约束< NSLayoutConstraint:0x600003370050'UISV-spacing'H:[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260e660]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260ecd0](活动)进行恢复在UIViewAlertForUnsatisfiableConstraints上创建一个符号断点,以在调试器中捕获该断点.< UIKitCore/UIView.h>中列出的UIView的UIConstraintBasedLayoutDebugging类别中的方法.可能也有帮助.2021-03-16 10:01:23.066504 + 1100 stackviewsWithProportionalFill [8528:4745644] [LayoutConstraints]无法同时满足约束.以下列表中的约束中至少有一个是您不想要的约束.尝试以下操作:(1)查看每个约束,并尝试找出不期望的约束;(2)查找添加了一个或多个不必要约束的代码并进行修复.(<< NSLayoutConstraint:0x60000332cb40 UIScrollView:0x7feb0381ea00.width == UIView:0x7feb0270aeb0.width(active)>,<< NSLayoutConstraint:0x60000332d270 UIStackView:0x7feb02709160.width == UIScrollView:0x7feb0381ea00.width(active)>< NSLayoutConstraint:0x600003371040'UISV-alignment'UILabel:0x7feb0270b2a0.leading == UIStackView:0x7feb02712130.Leading(active)>",< NSLayoutConstraint:0x600003371450'UISV-alignment'UILabel:0x7feb0270b2a0.trailing == UIStackView:0x7feb02712130.trailing(active)>",< NSLayoutConstraint:0x600003312e90'UISV-canvas-connection'UILayoutGuide:0x600002920b60'UIViewLayoutMarginsGuide'.leading == stackviewsWithProportionalFill.IconView:0x7feb027179a0.Leading(active)>,"< NSLayoutConstraint:0x600003312ee0'UISV-canvas-connection'UILayoutGuide:0x600002920b60'UIViewLayoutMarginsGuide'.trailing == stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260e1f0.trailing(active)>" ;,< NSLayoutConstraint:0x600003370c80'UISV-canvas-connection'UIStackView:0x7feb02709160.leading == UILabel:0x7feb0270b2a0.Leading(active)>",< NSLayoutConstraint:0x600003370cd0'UISV-canvas-connection'H:[UILabel:0x7feb0270b2a0]-(0)-|(活动,名称:"|":UIStackView:0x7feb02709160)>",< NSLayoutConstraint:0x600003313020'UISV-spacing'H:[stackviewsWithProportionalFill.IconView:0x7feb027179a0]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260e1f0](有效)>" ;,<< NSLayoutConstraint:0x600003371540'UIView-Encapsulated-Layout-Width'UIView:0x7feb0270aeb0.width == 0(active)>",< NSLayoutConstraint:0x600003312da0'UIView-leftMargin-guide-constraint'H:|-(14)-[UILayoutGuide:0x600002920b60'UIViewLayoutMarginsGuide'](LTR)(有效,名称:"|":UIStackView:0x7feb02712130)>",< NSLayoutConstraint:0x600003312e40'UIView-rightMargin-guide-constraint'H:[UILayoutGuide:0x600002920b60'UIViewLayoutMarginsGuide']-(15)-|(LTR)(活动,名称:"|":UIStackView:0x7feb02712130)>")将尝试通过打破约束< NSLayoutConstraint:0x600003313020'UISV-spacing'H:[stackviewsWithProportionalFill.IconView:0x7feb027179a0]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260e1f0](有效)进行恢复在UIViewAlertForUnsatisfiableConstraints上创建一个符号断点,以在调试器中捕获该断点.< UIKitCore/UIView.h>中列出的UIView的UIConstraintBasedLayoutDebugging类别中的方法.可能也有帮助.2021-03-16 10:01:23.067723 + 1100 stackviewsWithProportionalFill [8528:4745644] [LayoutConstraints]无法同时满足约束.以下列表中的约束中至少有一个是您不想要的约束.尝试以下操作:(1)查看每个约束,并尝试找出不期望的约束;(2)查找添加了一个或多个不必要约束的代码并进行修复.(<< NSLayoutConstraint:0x60000332cb40 UIScrollView:0x7feb0381ea00.width == UIView:0x7feb0270aeb0.width(active)>,<< NSLayoutConstraint:0x60000332d270 UIStackView:0x7feb02709160.width == UIScrollView:0x7feb0381ea00.width(active)>< NSLayoutConstraint:0x600003370f00'UISV-alignment'UILabel:0x7feb0270b2a0.leading == UIStackView:0x7feb02710b30.Leading(active)>",< NSLayoutConstraint:0x600003371310'UISV-alignment'UILabel:0x7feb0270b2a0.trailing == UIStackView:0x7feb02710b30.trailing(active)>",< NSLayoutConstraint:0x6000033118b0'UISV-canvas-connection'UILayoutGuide:0x600002920380'UIViewLayoutMarginsGuide'.leading == stackviewsWithProportionalFill.FixedLabel:0x7feb02716a60.leading(active)>,"< NSLayoutConstraint:0x600003311900'UISV-canvas-connection'UILayoutGuide:0x600002920380'UIViewLayoutMarginsGuide'.trailing == stackviewsWithProportionalFill.ExpandingLabel:0x7feb02716cd0.trailing(active)>,"< NSLayoutConstraint:0x600003370c80'UISV-canvas-connection'UIStackView:0x7feb02709160.leading == UILabel:0x7feb0270b2a0.Leading(active)>",< NSLayoutConstraint:0x600003370cd0'UISV-canvas-connection'H:[UILabel:0x7feb0270b2a0]-(0)-|(活动,名称:"|":UIStackView:0x7feb02709160)>",< NSLayoutConstraint:0x600003311a40'UISV-spacing'H:[stackviewsWithProportionalFill.FixedLabel:0x7feb02716a60]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb02716cd0](活动),",<< NSLayoutConstraint:0x600003371540'UIView-Encapsulated-Layout-Width'UIView:0x7feb0270aeb0.width == 0(active)>",< NSLayoutConstraint:0x6000033117c0'UIView-leftMargin-guide-constraint'H:|-(12)-[UILayoutGuide:0x600002920380'UIViewLayoutMarginsGuide'](LTR)(有效,名称:"|":UIStackView:0x7feb02710b30)>",< NSLayoutConstraint:0x600003311860'UIView-rightMargin-guide-constraint'H:[UILayoutGuide:0x600002920380'UIViewLayoutMarginsGuide']-(13)-|(LTR)(活动,名称:"|":UIStackView:0x7feb02710b30)>")将尝试通过打破约束< NSLayoutConstraint:0x600003311a40'UISV-spacing'H:[stackviewsWithProportionalFill.FixedLabel:0x7feb02716a60]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb02716cd0](处于活动状态)>在UIViewAlertForUnsatisfiableConstraints上创建一个符号断点,以在调试器中捕获该断点.< UIKitCore/UIView.h>中列出的UIView的UIConstraintBasedLayoutDebugging类别中的方法.可能也有帮助.2021-03-16 10:01:23.068649 + 1100 stackviewsWithProportionalFill [8528:4745644] [LayoutConstraints]无法同时满足约束.以下列表中的约束中至少有一个是您不想要的约束.尝试以下操作:(1)查看每个约束,并尝试找出不期望的约束;(2)查找添加了一个或多个不必要约束的代码并进行修复.(<< NSLayoutConstraint:0x60000332cb40 UIScrollView:0x7feb0381ea00.width == UIView:0x7feb0270aeb0.width(active)>,<< NSLayoutConstraint:0x60000332d270 UIStackView:0x7feb02709160.width == UIScrollView:0x7feb0381ea00.width(active)>< NSLayoutConstraint:0x600003370e60'UISV-alignment'UILabel:0x7feb0270b2a0.leading == UIStackView:0x7feb02710130.Leading(active)>",< NSLayoutConstraint:0x600003371270'UISV-alignment'UILabel:0x7feb0270b2a0.trailing == UIStackView:0x7feb02710130.trailing(active)>,< NSLayoutConstraint:0x600003316df0'UISV-canvas-connection'UILayoutGuide:0x600002939880'UIViewLayoutMarginsGuide'.leading == stackviewsWithProportionalFill.ExpandingLabel:0x7feb027167f0.Leading(活动)>,"< NSLayoutConstraint:0x600003316da0'UISV-canvas-connection'UILayoutGuide:0x600002939880'UIViewLayoutMarginsGuide'.trailing == stackviewsWithProportionalFill.ExpandingLabel:0x7feb027167f0.trailing(active)>,"< NSLayoutConstraint:0x600003370c80'UISV-canvas-connection'UIStackView:0x7feb02709160.leading == UILabel:0x7feb0270b2a0.Leading(active)>",< NSLayoutConstraint:0x600003370cd0'UISV-canvas-connection'H:[UILabel:0x7feb0270b2a0]-(0)-|(活动,名称:"|":UIStackView:0x7feb02709160)>",<< NSLayoutConstraint:0x600003371540'UIView-Encapsulated-Layout-Width'UIView:0x7feb0270aeb0.width == 0(active)>",< NSLayoutConstraint:0x600003316ee0'UIView-leftMargin-guide-constraint'H:|-(10)-[UILayoutGuide:0x600002939880'UIViewLayoutMarginsGuide'](LTR)(有效,名称:"|":UIStackView:0x7feb02710130)>",< NSLayoutConstraint:0x600003316e40'UIView-rightMargin-guide-constraint'H:[UILayoutGuide:0x600002939880'UIViewLayoutMarginsGuide']-(11)-|(LTR)(活动,名称:"|":UIStackView:0x7feb02710130)>")将尝试通过打破约束< NSLayoutConstraint:0x600003316da0'UISV-canvas-connection'UILayoutGuide:0x600002939880'UIViewLayoutMarginsGuide'.trailing == stackviewsWithProportionalFill.ExpandingLabel:0x7feb027167f0.trailing(active)>进行恢复在UIViewAlertForUnsatisfiableConstraints上创建一个符号断点,以在调试器中捕获该断点.< UIKitCore/UIView.h>中列出的UIView的UIConstraintBasedLayoutDebugging类别中的方法.可能也有帮助.2021-03-16 10:01:24.091972 + 1100 stackviewsWithProportionalFill [8528:4745811] [] nw_protocol_get_quic_image_block_invoke dlopen libquic失败
您无意中提出了我告诉人们的(许多)原因之一,*使您忘记了 .fillProportionally
分发属性 UIStackView
...
对于水平堆栈视图,您具有:
sv.distribution = .fill按比例
因此,您刚刚告诉自动布局:将所布置的子视图的宽度彼此成比例地 !"
然后您将标签添加为排列的子视图,并在标签上设置此属性:
setContentHuggingPriority(.required,for:.horizontal)
因此,您只是告诉自动布局:不要不要拉伸此标签的宽度!"
Kinda很难自动布局以同时拉伸和不拉伸视图:)
除非您确切知道为什么要 .fillProportionally
(在这种情况下,就是您想要的 不是 ),否则不要用它.对于您的布局,您需要默认的 .fill
.
因此,如果我们暂时忘记将 ChildVC
加载为子VC( grin )并设置 ChildVC
作为根"查看控制器,并保留 sv.distribution = .fillProportionally
,您将收到布局错误.
如果将其更改为 sv.distribution = .fill
,则不会出现布局错误.
但是,回到使用该VC作为子 ...甚至使用 .fill
,您仍然会遇到布局错误.所以...
更改您的加载孩子" ParentVC
中的代码为此:
class ParentVC:UIViewController {覆盖func viewDidLoad(){让vcContainer = UIView()view.addSubview(vcContainer)vcContainer.backgroundColor = .systemYellowvcContainer.translatesAutoresizingMaskIntoConstraints = falseNSLayoutConstraint.activate([vcContainer.topAnchor.constraint(等于:view.topAnchor,常数:100),vcContainer.bottomAnchor.constraint(equalTo:view.bottomAnchor,常数:-150),vcContainer.leadingAnchor.constraint(equalTo:view.leadingAnchor,常数:50),vcContainer.trailingAnchor.constraint(equalTo:view.trailingAnchor,常数:-50),])让vc = ChildVC()addChild(vc)vcContainer.addSubview(vc.view)vc.view.translatesAutoresizingMaskIntoConstraints = falseNSLayoutConstraint.activate([vc.view.topAnchor.constraint(等于:vcContainer.topAnchor),vc.view.leadingAnchor.constraint(等于:vcContainer.leadingAnchor),vc.view.trailingAnchor.constraint(等于:vcContainer.trailingAnchor),vc.view.bottomAnchor.constraint(等于:vcContainer.bottomAnchor),])vc.didMove(toParent:self)}}
该方法与 sv.distribution = .fill
结合使用,可以消除错误.
This question comes off the back of the great question and answer at:
I am having similar issues but with embedded stackviews inside a scrollview. I have modified the explanation code from the above question. In my working code, I have a parentVC that loads into a container view, a childVC that has the embedded scrollView and stackViews (outer stackview is vertical and then each of its subviews are a bunch of horizontal stackviews). There are a range of options from fixed width label, icons and expanding labels.
The problem is I either get UISV-spacing constraint errors or trailing errors for the expanding labels. I understand from the above question that this has to do with the sequence of how the auto layout engine is calculating the proportional widths, spacing etc. but have no idea how to fix. Any suggestions would be welcome. I'm attaching the code I'm using:
class ParentVC: UIViewController {
override func viewDidLoad() {
let vcContainer = UIView()
view.addSubview(vcContainer)
vcContainer.backgroundColor = .systemYellow
vcContainer.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
vcContainer.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
vcContainer.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -150),
vcContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 50),
vcContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -50),
])
let vc = ChildVC()
addChild(vc)
vc.didMove(toParent: self)
vcContainer.addSubview(vc.view)
vc.view.frame = vcContainer.bounds
vc.view.heightAnchor.constraint(equalTo: vcContainer.heightAnchor).isActive = true
}
}
class ChildVC: UIViewController {
let outerStackView: UIStackView = {
let v = UIStackView()
v.translatesAutoresizingMaskIntoConstraints = false
v.axis = .vertical
v.spacing = 8
return v
}()
let scrollView: UIScrollView = {
let sv = UIScrollView()
sv.translatesAutoresizingMaskIntoConstraints = false
return sv
}()
override func viewDidLoad() {
super.viewDidLoad()
for _ in 1...7 {
let lbl = UILabel()
lbl.font = UIFont.systemFont(ofSize: 12.0, weight: .light)
lbl.numberOfLines = 0
lbl.textAlignment = .center
outerStackView.addArrangedSubview(lbl)
let sv = UIStackView()
sv.translatesAutoresizingMaskIntoConstraints = false
sv.axis = .horizontal
sv.distribution = .fillProportionally
sv.spacing = 10
outerStackView.addArrangedSubview(sv)
}
view.addSubview(scrollView)
scrollView.addSubview(outerStackView)
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
scrollView.widthAnchor.constraint(equalTo: view.widthAnchor),
scrollView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
scrollView.heightAnchor.constraint(equalTo: view.heightAnchor),
outerStackView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
outerStackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
outerStackView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor),
outerStackView.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor),
outerStackView.widthAnchor.constraint(equalTo: scrollView.widthAnchor),
])
// StackView 1
if let lbl = outerStackView.arrangedSubviews[0] as? UILabel,
let sv = outerStackView.arrangedSubviews[1] as? UIStackView {
let v = ExpandingLabel()
sv.addArrangedSubview(v)
lbl.text = "SV1: One view \n no layoutMargins"
}
// StackView 2
if let lbl = outerStackView.arrangedSubviews[2] as? UILabel,
let sv = outerStackView.arrangedSubviews[3] as? UIStackView {
let v = FixedLabel()
sv.addArrangedSubview(v)
let v2 = ExpandingLabel()
sv.addArrangedSubview(v2)
lbl.text = "SV2: Two views \n no layoutMargins"
}
// comment out this block to see the auto-layout error goes away
// StackView 3
if let lbl = outerStackView.arrangedSubviews[4] as? UILabel,
let sv = outerStackView.arrangedSubviews[5] as? UIStackView {
let v = ExpandingLabel()
sv.addArrangedSubview(v)
sv.isLayoutMarginsRelativeArrangement = true
sv.layoutMargins = .init(top: 0, left: 10, bottom: 0, right: 11)
lbl.text = "SV3: One view\nlayoutMargins left: \(sv.layoutMargins.left), right: \(sv.layoutMargins.right)"
}
// StackView 4
if let lbl = outerStackView.arrangedSubviews[6] as? UILabel,
let sv = outerStackView.arrangedSubviews[7] as? UIStackView {
let v = FixedLabel()
sv.addArrangedSubview(v)
let v2 = ExpandingLabel()
sv.addArrangedSubview(v2)
sv.isLayoutMarginsRelativeArrangement = true
sv.layoutMargins = .init(top: 0, left: 12, bottom: 0, right: 13)
lbl.text = "SV4: Two views\nlayoutMargins\n left: \(sv.layoutMargins.left), right: \(sv.layoutMargins.right)"
}
// StackView 5
if let lbl = outerStackView.arrangedSubviews[8] as? UILabel,
let sv = outerStackView.arrangedSubviews[9] as? UIStackView {
let v = FixedLabel()
sv.addArrangedSubview(v)
let v2 = ExpandingLabel()
sv.addArrangedSubview(v2)
sv.isLayoutMarginsRelativeArrangement = true
sv.layoutMargins = .init(top: 0, left: 101, bottom: 0, right: 0)
lbl.text = "SV5: Two views \nlayoutMargins left: \(sv.layoutMargins.left), right: \(sv.layoutMargins.right)"
}
// StackView 6
if let lbl = outerStackView.arrangedSubviews[10] as? UILabel,
let sv = outerStackView.arrangedSubviews[11] as? UIStackView {
let v = IconView()
sv.addArrangedSubview(v)
let v2 = ExpandingLabel()
v2.numberOfLines = 0
sv.addArrangedSubview(v2)
sv.isLayoutMarginsRelativeArrangement = true
sv.layoutMargins = .init(top: 0, left: 14, bottom: 0, right: 15)
lbl.text = "SV6: Two views\nlayoutMargins left: \(sv.layoutMargins.left), right: \(sv.layoutMargins.right)"
}
// StackView 7
if let lbl = outerStackView.arrangedSubviews[12] as? UILabel,
let sv = outerStackView.arrangedSubviews[13] as? UIStackView {
let v = ExpandingLabel()
sv.addArrangedSubview(v)
sv.isLayoutMarginsRelativeArrangement = true
sv.layoutMargins = .init(top: 0, left: 16, bottom: 0, right: 17)
let v2 = ExpandingLabel()
sv.addArrangedSubview(v2)
lbl.text = "SV7: One view\nlayoutMargins left: \(sv.layoutMargins.left), right: \(sv.layoutMargins.right)"
}
}
}
class IconView: UIImageView {
init() {
super.init(frame: .zero)
self.backgroundColor = UIColor.systemRed
self.tintColor = .white
image = UIImage(systemName: "square.and.arrow.up")?.applyingSymbolConfiguration(UIImage.SymbolConfiguration(textStyle: .body))
contentMode = .center
translatesAutoresizingMaskIntoConstraints = false
adjustsImageSizeForAccessibilityContentSizeCategory = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class FixedLabel : UILabel {
override init(frame: CGRect) {
super.init(frame: frame)
text = "fxd"
backgroundColor = .systemRed
translatesAutoresizingMaskIntoConstraints = false
setContentCompressionResistancePriority(.required, for: .horizontal)
setContentHuggingPriority(.required, for: .horizontal)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class ExpandingLabel: UILabel {
override init(frame: CGRect) {
super.init(frame: frame)
text = "expanding label"
backgroundColor = .systemGreen
numberOfLines = 3
font = UIFont.preferredFont(forTextStyle: .body)
adjustsFontForContentSizeCategory = true
translatesAutoresizingMaskIntoConstraints = false
setContentHuggingPriority(.defaultLow, for: .horizontal)
setContentCompressionResistancePriority(.defaultHigh, for: .vertical)
setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
The above produces the desired result as per below:
The log errors are:
2021-03-16 10:01:22.815997+1100 stackviewsWithProportionalFill[8528:4745644] Sherlock initialised 2021-03-16 10:01:23.051125+1100 stackviewsWithProportionalFill[8528:4745644] [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:0x600003317e80 'UISV-canvas-connection' UIStackView:0x7feb0270f930.leading == stackviewsWithProportionalFill.FixedLabel:0x7feb02715f80.leading (active)>",
"<NSLayoutConstraint:0x600003317930 'UISV-canvas-connection' H:[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0270b710]-(0)-| (active, names: '|':UIStackView:0x7feb0270f930 )>",
"<NSLayoutConstraint:0x6000033172a0 'UISV-fill-proportionally' stackviewsWithProportionalFill.ExpandingLabel:0x7feb0270b710.width == UIStackView:0x7feb0270f930.width (active)>",
"<NSLayoutConstraint:0x6000033178e0 'UISV-spacing' H:[stackviewsWithProportionalFill.FixedLabel:0x7feb02715f80]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0270b710] (active)>" )
Will attempt to recover by breaking constraint <NSLayoutConstraint:0x6000033178e0 'UISV-spacing' H:[stackviewsWithProportionalFill.FixedLabel:0x7feb02715f80]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0270b710] (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful. 2021-03-16 10:01:23.057974+1100 stackviewsWithProportionalFill[8528:4745644] [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:0x60000332cb40 UIScrollView:0x7feb0381ea00.width == UIView:0x7feb0270aeb0.width (active)>",
"<NSLayoutConstraint:0x60000332d270 UIStackView:0x7feb02709160.width == UIScrollView:0x7feb0381ea00.width (active)>",
"<NSLayoutConstraint:0x600003370fa0 'UISV-alignment' UILabel:0x7feb0270b2a0.leading == UIStackView:0x7feb02711930.leading (active)>",
"<NSLayoutConstraint:0x6000033713b0 'UISV-alignment' UILabel:0x7feb0270b2a0.trailing == UIStackView:0x7feb02711930.trailing (active)>",
"<NSLayoutConstraint:0x600003310eb0 'UISV-canvas-connection' UILayoutGuide:0x6000029208c0'UIViewLayoutMarginsGuide'.leading == stackviewsWithProportionalFill.FixedLabel:0x7feb02717140.leading (active)>",
"<NSLayoutConstraint:0x6000033123f0 'UISV-canvas-connection' UILayoutGuide:0x6000029208c0'UIViewLayoutMarginsGuide'.trailing == stackviewsWithProportionalFill.ExpandingLabel:0x7feb02717530.trailing (active)>",
"<NSLayoutConstraint:0x600003370c80 'UISV-canvas-connection' UIStackView:0x7feb02709160.leading == UILabel:0x7feb0270b2a0.leading (active)>",
"<NSLayoutConstraint:0x600003370cd0 'UISV-canvas-connection' H:[UILabel:0x7feb0270b2a0]-(0)-| (active, names: '|':UIStackView:0x7feb02709160 )>",
"<NSLayoutConstraint:0x600003312530 'UISV-spacing' H:[stackviewsWithProportionalFill.FixedLabel:0x7feb02717140]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb02717530] (active)>",
"<NSLayoutConstraint:0x600003371540 'UIView-Encapsulated-Layout-Width' UIView:0x7feb0270aeb0.width == 0 (active)>",
"<NSLayoutConstraint:0x600003312300 'UIView-leftMargin-guide-constraint' H:|-(101)-[UILayoutGuide:0x6000029208c0'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':UIStackView:0x7feb02711930 )>",
"<NSLayoutConstraint:0x6000033123a0 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x6000029208c0'UIViewLayoutMarginsGuide']-(0)-|(LTR) (active, names: '|':UIStackView:0x7feb02711930 )>" )
Will attempt to recover by breaking constraint <NSLayoutConstraint:0x600003312530 'UISV-spacing' H:[stackviewsWithProportionalFill.FixedLabel:0x7feb02717140]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb02717530] (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful. 2021-03-16 10:01:23.065566+1100 stackviewsWithProportionalFill[8528:4745644] [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:0x60000332cb40 UIScrollView:0x7feb0381ea00.width == UIView:0x7feb0270aeb0.width (active)>",
"<NSLayoutConstraint:0x60000332d270 UIStackView:0x7feb02709160.width == UIScrollView:0x7feb0381ea00.width (active)>",
"<NSLayoutConstraint:0x6000033710e0 'UISV-alignment' UILabel:0x7feb0270b2a0.leading == UIStackView:0x7feb02712730.leading (active)>",
"<NSLayoutConstraint:0x6000033714f0 'UISV-alignment' UILabel:0x7feb0270b2a0.trailing == UIStackView:0x7feb02712730.trailing (active)>",
"<NSLayoutConstraint:0x6000033138e0 'UISV-canvas-connection' UILayoutGuide:0x600002920a80'UIViewLayoutMarginsGuide'.leading == stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260e660.leading (active)>",
"<NSLayoutConstraint:0x600003313930 'UISV-canvas-connection' UILayoutGuide:0x600002920a80'UIViewLayoutMarginsGuide'.trailing == stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260ecd0.trailing (active)>",
"<NSLayoutConstraint:0x600003370c80 'UISV-canvas-connection' UIStackView:0x7feb02709160.leading == UILabel:0x7feb0270b2a0.leading (active)>",
"<NSLayoutConstraint:0x600003370cd0 'UISV-canvas-connection' H:[UILabel:0x7feb0270b2a0]-(0)-| (active, names: '|':UIStackView:0x7feb02709160 )>",
"<NSLayoutConstraint:0x600003370050 'UISV-spacing' H:[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260e660]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260ecd0] (active)>",
"<NSLayoutConstraint:0x600003371540 'UIView-Encapsulated-Layout-Width' UIView:0x7feb0270aeb0.width == 0 (active)>",
"<NSLayoutConstraint:0x6000033137f0 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x600002920a80'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':UIStackView:0x7feb02712730 )>",
"<NSLayoutConstraint:0x600003313890 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600002920a80'UIViewLayoutMarginsGuide']-(17)-|(LTR) (active, names: '|':UIStackView:0x7feb02712730 )>" )
Will attempt to recover by breaking constraint <NSLayoutConstraint:0x600003370050 'UISV-spacing' H:[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260e660]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260ecd0] (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful. 2021-03-16 10:01:23.066504+1100 stackviewsWithProportionalFill[8528:4745644] [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:0x60000332cb40 UIScrollView:0x7feb0381ea00.width == UIView:0x7feb0270aeb0.width (active)>",
"<NSLayoutConstraint:0x60000332d270 UIStackView:0x7feb02709160.width == UIScrollView:0x7feb0381ea00.width (active)>",
"<NSLayoutConstraint:0x600003371040 'UISV-alignment' UILabel:0x7feb0270b2a0.leading == UIStackView:0x7feb02712130.leading (active)>",
"<NSLayoutConstraint:0x600003371450 'UISV-alignment' UILabel:0x7feb0270b2a0.trailing == UIStackView:0x7feb02712130.trailing (active)>",
"<NSLayoutConstraint:0x600003312e90 'UISV-canvas-connection' UILayoutGuide:0x600002920b60'UIViewLayoutMarginsGuide'.leading == stackviewsWithProportionalFill.IconView:0x7feb027179a0.leading (active)>",
"<NSLayoutConstraint:0x600003312ee0 'UISV-canvas-connection' UILayoutGuide:0x600002920b60'UIViewLayoutMarginsGuide'.trailing == stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260e1f0.trailing (active)>",
"<NSLayoutConstraint:0x600003370c80 'UISV-canvas-connection' UIStackView:0x7feb02709160.leading == UILabel:0x7feb0270b2a0.leading (active)>",
"<NSLayoutConstraint:0x600003370cd0 'UISV-canvas-connection' H:[UILabel:0x7feb0270b2a0]-(0)-| (active, names: '|':UIStackView:0x7feb02709160 )>",
"<NSLayoutConstraint:0x600003313020 'UISV-spacing' H:[stackviewsWithProportionalFill.IconView:0x7feb027179a0]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260e1f0] (active)>",
"<NSLayoutConstraint:0x600003371540 'UIView-Encapsulated-Layout-Width' UIView:0x7feb0270aeb0.width == 0 (active)>",
"<NSLayoutConstraint:0x600003312da0 'UIView-leftMargin-guide-constraint' H:|-(14)-[UILayoutGuide:0x600002920b60'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':UIStackView:0x7feb02712130 )>",
"<NSLayoutConstraint:0x600003312e40 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600002920b60'UIViewLayoutMarginsGuide']-(15)-|(LTR) (active, names: '|':UIStackView:0x7feb02712130 )>" )
Will attempt to recover by breaking constraint <NSLayoutConstraint:0x600003313020 'UISV-spacing' H:[stackviewsWithProportionalFill.IconView:0x7feb027179a0]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb0260e1f0] (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful. 2021-03-16 10:01:23.067723+1100 stackviewsWithProportionalFill[8528:4745644] [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:0x60000332cb40 UIScrollView:0x7feb0381ea00.width == UIView:0x7feb0270aeb0.width (active)>",
"<NSLayoutConstraint:0x60000332d270 UIStackView:0x7feb02709160.width == UIScrollView:0x7feb0381ea00.width (active)>",
"<NSLayoutConstraint:0x600003370f00 'UISV-alignment' UILabel:0x7feb0270b2a0.leading == UIStackView:0x7feb02710b30.leading (active)>",
"<NSLayoutConstraint:0x600003371310 'UISV-alignment' UILabel:0x7feb0270b2a0.trailing == UIStackView:0x7feb02710b30.trailing (active)>",
"<NSLayoutConstraint:0x6000033118b0 'UISV-canvas-connection' UILayoutGuide:0x600002920380'UIViewLayoutMarginsGuide'.leading == stackviewsWithProportionalFill.FixedLabel:0x7feb02716a60.leading (active)>",
"<NSLayoutConstraint:0x600003311900 'UISV-canvas-connection' UILayoutGuide:0x600002920380'UIViewLayoutMarginsGuide'.trailing == stackviewsWithProportionalFill.ExpandingLabel:0x7feb02716cd0.trailing (active)>",
"<NSLayoutConstraint:0x600003370c80 'UISV-canvas-connection' UIStackView:0x7feb02709160.leading == UILabel:0x7feb0270b2a0.leading (active)>",
"<NSLayoutConstraint:0x600003370cd0 'UISV-canvas-connection' H:[UILabel:0x7feb0270b2a0]-(0)-| (active, names: '|':UIStackView:0x7feb02709160 )>",
"<NSLayoutConstraint:0x600003311a40 'UISV-spacing' H:[stackviewsWithProportionalFill.FixedLabel:0x7feb02716a60]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb02716cd0] (active)>",
"<NSLayoutConstraint:0x600003371540 'UIView-Encapsulated-Layout-Width' UIView:0x7feb0270aeb0.width == 0 (active)>",
"<NSLayoutConstraint:0x6000033117c0 'UIView-leftMargin-guide-constraint' H:|-(12)-[UILayoutGuide:0x600002920380'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':UIStackView:0x7feb02710b30 )>",
"<NSLayoutConstraint:0x600003311860 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600002920380'UIViewLayoutMarginsGuide']-(13)-|(LTR) (active, names: '|':UIStackView:0x7feb02710b30 )>" )
Will attempt to recover by breaking constraint <NSLayoutConstraint:0x600003311a40 'UISV-spacing' H:[stackviewsWithProportionalFill.FixedLabel:0x7feb02716a60]-(10)-[stackviewsWithProportionalFill.ExpandingLabel:0x7feb02716cd0] (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful. 2021-03-16 10:01:23.068649+1100 stackviewsWithProportionalFill[8528:4745644] [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:0x60000332cb40 UIScrollView:0x7feb0381ea00.width == UIView:0x7feb0270aeb0.width (active)>",
"<NSLayoutConstraint:0x60000332d270 UIStackView:0x7feb02709160.width == UIScrollView:0x7feb0381ea00.width (active)>",
"<NSLayoutConstraint:0x600003370e60 'UISV-alignment' UILabel:0x7feb0270b2a0.leading == UIStackView:0x7feb02710130.leading (active)>",
"<NSLayoutConstraint:0x600003371270 'UISV-alignment' UILabel:0x7feb0270b2a0.trailing == UIStackView:0x7feb02710130.trailing (active)>",
"<NSLayoutConstraint:0x600003316df0 'UISV-canvas-connection' UILayoutGuide:0x600002939880'UIViewLayoutMarginsGuide'.leading == stackviewsWithProportionalFill.ExpandingLabel:0x7feb027167f0.leading (active)>",
"<NSLayoutConstraint:0x600003316da0 'UISV-canvas-connection' UILayoutGuide:0x600002939880'UIViewLayoutMarginsGuide'.trailing == stackviewsWithProportionalFill.ExpandingLabel:0x7feb027167f0.trailing (active)>",
"<NSLayoutConstraint:0x600003370c80 'UISV-canvas-connection' UIStackView:0x7feb02709160.leading == UILabel:0x7feb0270b2a0.leading (active)>",
"<NSLayoutConstraint:0x600003370cd0 'UISV-canvas-connection' H:[UILabel:0x7feb0270b2a0]-(0)-| (active, names: '|':UIStackView:0x7feb02709160 )>",
"<NSLayoutConstraint:0x600003371540 'UIView-Encapsulated-Layout-Width' UIView:0x7feb0270aeb0.width == 0 (active)>",
"<NSLayoutConstraint:0x600003316ee0 'UIView-leftMargin-guide-constraint' H:|-(10)-[UILayoutGuide:0x600002939880'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':UIStackView:0x7feb02710130 )>",
"<NSLayoutConstraint:0x600003316e40 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600002939880'UIViewLayoutMarginsGuide']-(11)-|(LTR) (active, names: '|':UIStackView:0x7feb02710130 )>" )
Will attempt to recover by breaking constraint <NSLayoutConstraint:0x600003316da0 'UISV-canvas-connection' UILayoutGuide:0x600002939880'UIViewLayoutMarginsGuide'.trailing == stackviewsWithProportionalFill.ExpandingLabel:0x7feb027167f0.trailing (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful. 2021-03-16 10:01:24.091972+1100 stackviewsWithProportionalFill[8528:4745811] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed
You have inadvertently presented one of the (many) reasons I tell people to *forget you ever heard of the .fillProportionally
Distribution property of a UIStackView
...
For your horizontal stack views, you have:
sv.distribution = .fillProportionally
So you just told auto-layout: "Stretch the widths of the arranged subviews proportionally to each other!"
You then add a label as an arranged subview, and you set this property on the label:
setContentHuggingPriority(.required, for: .horizontal)
So you just told auto-layout: "Do NOT stretch the width of this label!"
Kinda tough for auto-layout to both stretch and not stretch the view at the same time :)
Unless you know exactly why you want .fillProportionally
(and in this case, that is not what you want), don't use it. For your layout, you want the default .fill
.
So, if we forget for a moment that you are loading ChildVC
as a, well, child VC (grin) and set ChildVC
as the "root" view controller, and leave sv.distribution = .fillProportionally
, you'll get layout errors.
If you change that to sv.distribution = .fill
, you won't get layout errors.
But, back to using that VC as a child ... even with .fill
you'll still get layout errors. So...
Change your "load the child" code in ParentVC
to this:
class ParentVC: UIViewController {
override func viewDidLoad() {
let vcContainer = UIView()
view.addSubview(vcContainer)
vcContainer.backgroundColor = .systemYellow
vcContainer.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
vcContainer.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
vcContainer.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -150),
vcContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 50),
vcContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -50),
])
let vc = ChildVC()
addChild(vc)
vcContainer.addSubview(vc.view)
vc.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
vc.view.topAnchor.constraint(equalTo: vcContainer.topAnchor),
vc.view.leadingAnchor.constraint(equalTo: vcContainer.leadingAnchor),
vc.view.trailingAnchor.constraint(equalTo: vcContainer.trailingAnchor),
vc.view.bottomAnchor.constraint(equalTo: vcContainer.bottomAnchor),
])
vc.didMove(toParent: self)
}
}
That approach, combined with sv.distribution = .fill
, gets rid of the errors.
这篇关于为什么嵌入式UIStackView(按比例填充)具有间距和布局边距,会导致约束错误以及如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!