When I hide a UIView
inside UIStackview
, on iOS 11 it nicely removes the gap and brings the adjacent UIViews
closer inside the UIStackView
. However, on iOS 10 it will not move the adjacent views and just leave the gap for the hidden view. Is there a way to get around it without having to manually move the views.
In order to hide a view within a stack view, set the contained view's hidden property to true , and the stack view handles the rest. This is how you'll fix the spacing under the WEATHER label when the user hides the text below it.
Overview. Stack views let you leverage the power of Auto Layout, creating user interfaces that can dynamically adapt to the device's orientation, screen size, and any changes in the available space. The stack view manages the layout of all the views in its arrangedSubviews property.
fill. A layout where the stack view resizes its arranged views so that they fill the available space perpendicular to the stack view's axis. The simplest of the alignments, . fill constrains the leading/trailing edges of each arranged subview to the leading/trailing edge of the stack view.
In order to hide a view within a stack view, set the contained view’s hidden property to true, and the stack view handles the rest. This is how you’ll fix the spacing under the WEATHER label when the user hides the text below it.
In iOS, the view’s hidden property is normally not animatable. However, this property becomes animatable for views as soon as they are placed in a stack’s arranged views array. The actual animation is managed by the stack, not the view. Use the hidden property to animate adding views to or removing them from the stack.
Align any contained views inside a stack view by using a combination of the alignment property, Spacer, and Divider views. In the previous example layout, the VStack that contains the two Text views uses the leading alignment: The alignment property doesn’t position the VStack inside its container; instead, it positions the views inside the VStack.
While the Stack View has total control over the layout of any subview (based on properties such as Axis and Distribution ), you still need to position the Stack View ( UIStackView) within its parent view using Auto Layout and Size Classes.
Have you added your own height constraint to the UIView? It will cause a conflict with the constraint height = 0
generated by the UIStackView when the UIView is hidden on iOS 10. Just lower the Priority of UIView height constraint (e.g. 999) and the UI will update properly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With