简体   繁体   English

在堆栈视图中设置某些项目之间的空间

[英]Setting space between some items in stack view

In Interface Builder, I use a horizontal UIStackView and want to place the items as this :在界面生成器中,我使用水平 UIStackView 并希望将项目放置为:

   xx   xx   xx

every x symbolize an item.每个 x 代表一个项目。 Item1 and item2 should be stuck one to the other, but item2 and item3 should be spaced and so on.项目 1 和项目 2 应该相互粘在一起,但项目 2 和项目 3 应该间隔开,依此类推。 That means I do not want constant spacing between all the items.这意味着我不希望所有项目之间保持恒定的间距。

How can I do so ?我怎么能这样做?
(maybe an equivalent of flexible space of toolbars) (可能相当于工具栏的灵活空间)

Thanks !谢谢 !

It's an answer based on Lumialxk's reply.这是基于 Lumialxk 回复的答案。 Choose the stack view you want to work on, and change the spacing property to your desired value.选择您想要处理的堆栈视图,并将间距属性更改为您想要的值。

属性面板的屏幕截图

iOS 11 and above iOS 11 及以上

stackView.setCustomSpacing(32.0, after: headerLabel)

For more info: Stack View Custom Spacing有关更多信息: 堆栈视图自定义间距

Inside your horizontal stack view have three more horizontal stack views so that you can have control over spacing and distribution for each item.在您的水平堆栈视图中还有三个水平堆栈视图,以便您可以控制每个项目的间距和分布。 See screenshot below:请看下面的截图:

在此处输入图片说明

Programmatically for each view :以编程方式为每个视图

stackView.spacing = 10.0

If you want to make a custom spacing after each view and you need to support lower than iOS11 :如果要在每个视图后自定义间距并且需要支持低于 iOS11

Create a clear UIView with height and add it as an addArrangedSubview into the stackView.创建一个具有高度的清晰 UIView 并将其作为addArrangedSubview添加到 stackView 中。

Swift 4:例如,如果您想在第一个子视图之后设置一个空格:

stackView.setCustomSpacing(30.0, after: stackView.subviews[0])

在stack view中,你只能定义items之间的恒定空间,做这样的事情很容易,只需要在outline中选择stack view,设置space的值即可。

This is not a conventional solution but it works so far as I have being testing.这不是传统的解决方案,但就我进行测试而言,它是有效的。 After setting a constant spacing between items for the stackview in interface builder, you can still add a constraint to a stackview item from its top to the bottom of item above it and give a different value from the constant spacing of the stackview.在interface builder中为stackview的items之间设置一个恒定的间距后,你仍然可以从它的顶部到它上面的item的底部给一个stackview item添加一个约束,并给出一个与stackview的恒定间距不同的值。 xcode will indicate an error because of conflicting constraints between the constraint it creates by setting the constant spacing of stackview and the constraint you have added. xcode 将指示错误,因为它通过设置 stackview 的恒定间距和您添加的约束创建的约束之间存在冲突约束。 but at run time it uses the second constraint added which gives a variable spacing from the constant spacing.但在运行时,它使用添加的第二个约束,该约束从恒定间距给出可变间距。

I think xcode interface is simply lagging behind and should be updated to not show this error because if the method stackView.setCustomSpacing exist already why is xcode not yet able to know at compile time that a variable spacing can exist.我认为 xcode 接口只是落后,应该更新以不显示此错误,因为如果方法 stackView.setCustomSpacing 已经存在,为什么 xcode 在编译时还不能知道可变间距可以存在。 But like I said the error is showed is showed but the second constraint is used.但是就像我说的那样显示错误,但使用了第二个约束。 Just try if it works as I described.试试看它是否像我描述的那样工作。 No guarantees!没有任何保证!

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

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