iOS:更改StackView(在StackView内部)中TextField的高度 [英] iOS: Change height of TextField in StackView (inside StackView)
问题描述
我正在使用Xcode 10/Swift 5/iOS 12并获得以下布局:
I'm using Xcode 10/Swift 5/iOS 12 and got this layout:
红色(垂直)StackView设置为:
The red (vertical) StackView is set to:
- 分发:均等填充"
- 高度:300(固定)
- 框架:10(每侧)
- 间距:10
5个水平子StackViews:
5 horizontal sub-StackViews:
- 分发:均等填充"
- 每个像素高52((300-40)/5)-不固定!
蓝色视图仅占用剩余的屏幕空间(顶部与红色StackView对齐,底部与"SuperView"对齐).
The blue View simply takes up the remaining screen space (top aligned to the red StackView and bottom to "SuperView").
标签会自动占用52个像素,但我希望两个TextField恰好是30个像素(上下分别为+11).如果我只是设置高度,InterfaceBuilder会抱怨发生冲突,并且实际上没有进行任何更改.
The labels automatically take up the 52 pixels but I want the two TextFields to be exactly 30 pixels (+11 above/below). If I just set the height, InterfaceBuilder complains about a conflict and doesn't actually change anything.
如何在不更改周围水平StackView高度的情况下设置其高度?
How do I set their height without changing the height of the surrounding horizontal StackView?
推荐答案
这里是一种解决方案-假设您希望堆栈视图中的每个行"高52磅(标签具有绿色背景,只是为了使其框架更容易看到).
Here is one solution - assuming you want each "row" in your stack view to be 52-pts tall (labels have green background, just to make their frames easier to see).
红色堆栈视图:
Alignment: Fill
Distribution: Fill Equally
Spacing: 10
每个水平堆栈视图:
Alignment: Center
Distribution: Fill Equally
Spacing: 0
Red View
-包含 Red Stack View
和 Blue View
-在所有四个方面都限制为从零到SuperView.
Red View
- which holds Red Stack View
and Blue View
- is constrained at Zero to SuperView on all four sides.
红色堆栈视图
受限制在顶部,导致并落后于其10点的SuperView(即 Red View
).
Red Stack View
is constrained Top, Leading and Trailing to its SuperView (which is Red View
) at 10-pts.
Blue View
在其SuperView( Red View
)的前导点,尾随点和底部点被限制在10点处,顶部在10点点处被限制在红色堆栈视图
.
Blue View
is constrained Leading, Trailing and Bottom to its SuperView (which is Red View
) at 10-pts, and Top at 10-pts to Bottom of Red Stack View
.
唯一的高度约束是在 Date Stack View
... Height = 52
上设置的.由于红色堆栈视图
的分布设置为均等填充
,因此其余的水平堆栈视图将自动获得52的高度.
The only Height constraint is set on Date Stack View
... Height = 52
. Since Red Stack View
has its distribution set to Fill Equally
the remaining horizontal stack views will automatically get heights of 52.
然后,通过在每个水平堆栈视图上设置 Alignment:Center
,排列的子视图将居中对齐到52磅的高度.
And, by setting Alignment: Center
on each horizontal stack view, the arranged subviews will be center-aligned to that 52-pt height.
这篇关于iOS:更改StackView(在StackView内部)中TextField的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!