Setting space between some items in stack view

IosXcode

Ios Problem Overview


In Interface Builder, I use a horizontal UIStackView and want to place the items as this :

   xx   xx   xx

every x symbolize an item. Item1 and item2 should be stuck one to the other, but item2 and item3 should be spaced and so on. 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 !

Ios Solutions


Solution 1 - Ios

iOS 11 and above

stackView.setCustomSpacing(32.0, after: headerLabel)

For more info: [Stack View Custom Spacing][1]

[1]: https://useyourloaf.com/blog/stack-view-custom-spacing/ "Stack View Custom Spacing"

Solution 2 - Ios

It's an answer based on Lumialxk's reply. Choose the stack view you want to work on, and change the spacing property to your desired value.

Screen shot of the property panel

Solution 3 - Ios

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:

enter image description here

Solution 4 - Ios

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:

Create a clear UIView with height and add it as an addArrangedSubview into the stackView.

Solution 5 - Ios

Swift 4 : If you want for example set a space after the first subview :

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

Solution 6 - Ios

In a stack view,you can only define the constant space between items.It's easy to do such things,just choose the stack view in the outline and set the value of space.

Solution 7 - Ios

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. 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. 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. 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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNahoutoView Question on Stackoverflow
Solution 1 - IosWarif Akhand RishiView Answer on Stackoverflow
Solution 2 - IosQ LiuView Answer on Stackoverflow
Solution 3 - IosAjay KumarView Answer on Stackoverflow
Solution 4 - IosTung FamView Answer on Stackoverflow
Solution 5 - IosFox5150View Answer on Stackoverflow
Solution 6 - IosLumialxkView Answer on Stackoverflow
Solution 7 - IosNjuacha HubertView Answer on Stackoverflow