简体   繁体   English

UIStackView 分布,一个子视图的内在内容大小优先

[英]UIStackView distribution with intrinsic content size of one subview prioritised

  stackView
+------------+                +
|  topView   | height: 50     |
+------------+                |
|            |                |
|            |                | total height: 300
| bottomView | height: 250    |
|            |                |
|            |                |
+------------+                +

I would like to layout two views ( topView and bottomView ) in a UIStackView ( stackView ) with these conditions:我想在具有以下条件的UIStackViewstackView )中布局两个视图( topViewbottomView ):

  • stackView has a height (let's say 300) stackView有一个高度(比如说 300)
  • topView 's and bottomView 's intrinsic content height is the same (let's say 50) topViewbottomView的内在内容高度相同(比如说 50)
  • topView 's should be rendered with its intrinsic content height (up to stackView 's height) (so 50) topView应该以其固有的内容高度呈现(直到stackView的高度)(所以 50)
  • bottomView should take the remaining space (so 250) bottomView应该占用剩余空间(所以 250)
  • if topView 's intrinsic content height changes (let's say to 100) the distribution should be 100/200如果topView的内在内容高度发生变化(假设为 100),则分布应为 100/200

None of the distribution modes of UIStackView can cover this. UIStackView 的所有分发模式都无法涵盖这一点。

Setting a constraint with a constant wouldn't help here, as the intrinsic content height of topView might change.在这里使用常量设置约束无济于事,因为topView的内在内容高度可能会改变。

So I need some Auto Layout features here.所以我需要一些自动布局功能。 I tried setting different content compression resistance priorities and content hugging priorities.我尝试设置不同的内容压缩阻力优先级和内容拥抱优先级。 Without success, I still feel like they could be a solution (if I knew how to use them correctly).如果没有成功,我仍然觉得它们可以成为一个解决方案(如果我知道如何正确使用它们)。

It doesn't seem like a complex problem, I can't find the right Auto Layout API for this.这似乎不是一个复杂的问题,为此我找不到合适的 Auto Layout API。 Any hints?有什么提示吗?

You should set:你应该设置:

  • vertical content hugging priority of top view to be more than bottom view垂直内容拥抱顶视图的优先级高于底视图
  • alignment of stack view to fill alignment 的堆栈视图填充
  • distribution of stack view to fill要填充的堆栈视图的分布

You should add a width constraint as well, if the top and bottom views do not have intrinsic width.如果顶部和底部视图没有固有宽度,您还应该添加宽度约束。

Demo:演示:

在此处输入图像描述 在此处输入图像描述

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

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