ios 如何将前导填充添加到 UIStackView 中添加的视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32551890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add leading padding to view added inside an UIStackView
提问by Adrian
This is my setup: I have an UIScrollView
with leading,top, trialing edge set to 0. Inside this I add an UIStackView
with this constraints:
这是我的设置:我有一个UIScrollView
前导、顶部、试验边缘设置为 0。在这个里面我添加了一个UIStackView
具有以下约束的:
stackView.centerYAnchor.constraintEqualToAnchor(selectedContactsScrollView.centerYAnchor).active = true
stackView.leadingAnchor.constraintEqualToAnchor(selectedContactsScrollView.leadingAnchor).active = true
Inside the stack view I add some views.
My issue is that because of the constraints the first view added to stack view will also have leading edge = 0.
在堆栈视图中,我添加了一些视图。
我的问题是,由于限制,添加到堆栈视图的第一个视图也将具有前沿 = 0。
What are the ways that I could add some padding to the first view ? Without adjusting the scroll view constraints.
我可以向第一个视图添加一些填充的方法是什么?无需调整滚动视图约束。
采纳答案by William Kinaan
The solution you have provided doesn't add a padding for your views inside your UIStackView (as you wanted in the question), but it adds a leading for the UIStackView.
您提供的解决方案不会在 UIStackView 中为您的视图添加填充(正如您在问题中想要的那样),但它为 UIStackView 添加了前导。
A solution could be to add another UIStackView inside your original UIStackView and give the leading to this new UIStackVIew. Then, add your views to this new UIStackView.
解决方案可能是在原始 UIStackView 中添加另一个 UIStackView 并为这个新的 UIStackVIew 提供引导。然后,将您的视图添加到这个新的 UIStackView。
Hint, you can do that completely using Interface Builder. In other words, no need to write code for it.
提示,您可以使用 Interface Builder 完全做到这一点。换句话说,不需要为它编写代码。
回答by Tolga Okur
When isLayoutMarginsRelativeArrangement
property is true, the stack view will layout its arranged views relative to its layout margins.
当isLayoutMarginsRelativeArrangement
属性为 true 时,堆栈视图将相对于其布局边距布局其排列的视图。
stackView.layoutMargins = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
stackView.isLayoutMarginsRelativeArrangement = true
But it affects all arranged views inside to the stack view. If you want this padding for only one arranged view, you need to use nested UIStackView
但它会影响堆栈视图中的所有排列视图。如果您只希望此填充用于一个排列的视图,则需要使用嵌套UIStackView
回答by Zaporozhchenko Oleksandr
I have found that constraints don't work inside a Stack View, or they seem somewhat strange.
我发现约束在堆栈视图中不起作用,或者它们看起来有些奇怪。
(Such as if I add leading/trailing constraint to selected on image stackview, that adds leading to collectionview too, but doesn't add trailing; and it be conflict for sure).
(例如,如果我向图像堆栈视图上的选定项添加前导/尾随约束,也会添加到集合视图的前导/尾随约束,但不会添加尾随;这肯定会发生冲突)。
To set layout margins for all views inside the stackview, select:
要为 stackview 内的所有视图设置布局边距,请选择:
Stack View > Size Inspector > Layout Margins > Fixed
Stack View > Size Inspector > Layout Margins > Fixed
Note: "Fixed"
option was formerly called "Explicit"
, as seen in the screenshots.
注意:"Fixed"
option 以前称为"Explicit"
,如屏幕截图所示。
Then add your padding:
然后添加你的填充:
回答by dorsz
What worked for me is to add to stack view another UIView that's just a spacer (works at least with stackView.distribution = .Fill):
对我有用的是将另一个 UIView 添加到堆栈视图中,它只是一个垫片(至少与 stackView.distribution = .Fill 一起使用):
let spacerView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
stackView.addArrangedSubview(spacerView)
stackView.addArrangedSubview(viewThatNeedsSpaceBeforeIt)
stackView.addArrangedSubview(NextView)...
回答by William Hu
swift 3: You just need set offset by:
swift 3:您只需要通过以下方式设置偏移量:
firstView.leadingAnchor.constraint(equalTo: parentView.leadingAnchor, constant: 200).isActive = true
Be sure this constraint set after you parentView.addArrangdSubView(firstView)
确保在您之后设置此约束 parentView.addArrangdSubView(firstView)
回答by Jonathan Parham
If you only need leading padding, then you can set the stack view's Alignment to "Trailing" and then you will be free to specify unique Leading constraints on each of its contained subviews.
如果您只需要前导填充,那么您可以将堆栈视图的对齐设置为“尾随”,然后您就可以自由地在其包含的每个子视图上指定唯一的前导约束。
As a bonus, you can also set the stack view's alignment to "Center" and then you can use Leading and/or Trailing constraints to give each item its own padding on both sides.
作为奖励,您还可以将堆栈视图的对齐方式设置为“居中”,然后您可以使用前导和/或尾随约束为每个项目在两侧提供自己的填充。
回答by infiniteLoop
This question already has good answers,
One suggestion though, use spacing
property to set the spacing between the views. For first and last views there can be two options, either set insets as @tolpp suggested or add constraint attaching to parent (stackview) with constant to add padding.
这个问题已经有了很好的答案,不过有一个建议,使用spacing
属性来设置视图之间的间距。对于第一个和最后一个视图,可以有两个选项,要么按照@tolpp 建议设置插入,要么添加附加到父级(堆栈视图)的约束,并使用常量添加填充。
回答by Aiden Dixon
The solution would be to have a regular view in the stack view to hold whatever views you are wanting to add constraints to, and then you can add constraints for your items that are relative to the views in the stack view. That way, your original views can have leading and trailing constraints within the stack view.
解决方案是在堆栈视图中有一个常规视图来保存您想要向其添加约束的任何视图,然后您可以为与堆栈视图中的视图相关的项目添加约束。这样,您的原始视图可以在堆栈视图中具有前导和尾随约束。
This can be done in the interface builder and programatically.
这可以在界面构建器中以编程方式完成。
回答by Crashalot
What we did was add transparent components (e.g., UIButton/UIView) as the first and last children of the UIStackView. Then set constrain the width of these invisible children to adjust the padding.
我们所做的是添加透明组件(例如,UIButton/UIView)作为 UIStackView 的第一个和最后一个孩子。然后设置约束这些不可见子项的宽度以调整填充。
回答by Adrian
It seems that the solution was pretty simple. Instead of:
似乎解决方案非常简单。代替:
stackView.leadingAnchor.constraintEqualToAnchor(selectedContactsScrollView.leadingAnchor).active = true
I just wrote:
我刚刚写道:
stackView.leadingAnchor.constraintEqualToAnchor(selectedContactsScrollView.leadingAnchor, constant: 15).active = true