Instance Property

spacing

The minimum spacing, in points, between adjacent views in the stack view.

Declaration

var spacing: CGFloat { get set }

Discussion

A stack view uses this property to define the minimum distance between views within a gravity area and between neighboring views in adjacent gravity areas. The default value for the spacing property is 8.0 points.

The automatically applied Auto Layout constraints for spacing are shown in Table 1.

Table 1

Default Auto Layout constraints for view spacing

Constraint

Value for constraint priority

inter-view spacing == the spacing property value

max (NSLayoutPriorityDefaultHigh, hugging priority)

inter-gravity-area spacing == the spacing property value

hugging priority

inter-view spacing the spacing property value

NSLayoutPriorityRequired

The first row in Table 1 indicates that inter-view spacing is constrained to equal the value of the spacing property with a priority of at least NSLayoutPriorityDefaultHigh; you can increase this by setting a higher stack view hugging priority with the setHuggingPriority(_:for:) method.

The second row in Table 1 indicates that the spacing between adjacent views in neighboring gravity areas is constrained to equal the value of the spacing property with the priority of the stack view’s hugging priority.

The third row in Table 1 indicates that inter-view spacing is allowed to grow larger than the value of the spacing property with a priority of NSLayoutPriorityRequired.

In combination, these constraints result in the following typical stack view behavior: In a stack view whose hasEqualSpacing property is set to false (the default) and whose hugging priority is left at NSLayoutPriorityDefaultLow (the default), views within a gravity area remain a fixed distance from each other (equal to the value of the spacing property), and the distance between gravity areas grows and shrinks as the stack view grows and shrinks along its layout direction axis. If you set the hasEqualSpacing property to true and use the default hugging priority, then the distance between all adjacent views, including adjacent views in neighboring gravity areas, grows and shrinks as the stack view grows and shrinks.

See Also

Configuring the Stack View Layout

var orientation: NSUserInterfaceLayoutOrientation

The horizontal or vertical layout direction of the stack view.

enum NSUserInterfaceLayoutOrientation

The two stack view layout directions and, for clipping resistance and hugging priority, the two user interface axes.

var alignment: NSLayoutConstraint.Attribute

The view alignment within the stack view.

var edgeInsets: NSEdgeInsets

The geometric padding inside the stack view, surrounding its views, in points.

var hasEqualSpacing: Bool

A Boolean value that indicates whether the spacing between adjacent views should equal each other.

Deprecated