How can one programmatically reorder items within a UIStackView in IOS?
(i.e. I can not see a function at https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIStackView_Class_Reference/#//apple_ref/doc/uid/TP40015256-CH1-SW30 to cover this)???
Or you can do the reordering in the code via using UIStackView ‘s arrangedSubviews property. This array gives you the current subviews in the stack view and by using insertArrangedSubview and removeArrangedSubview every items’ order can be manipulated.
The UIStackView is a nonrendering subclass of UIView; that is, it does not provide any user interface of its own. Instead, it just manages the position and size of its arranged views. As a result, some properties (like backgroundColor) have no effect on the stack view.
To use a stack view, open the Storyboard you wish to edit. Drag either a Horizontal Stack View or a Vertical Stack View out from the Object library, and position the stack view where desired. Next, drag out the stack’s content, dropping the view or control into the stack. You can continue to add views and controls to your stack, as needed.
When the stack view adds a view to its arrangedSubviews array, it also adds that view as a subview, if it isn’t already. When a subview is removed from the stack view, the stack view also removes it from the arrangedSubviews array.
When adding a view to a parent view, it is automatically removed from it's old parent view.
This is the same with arrangedView
, so you only need to use insertArrangedSubview(_:atIndex:)
and addArrangedSubview()
.
Using removeArrangedSubview()
is not needed.
surely you can use a combination of
removeArrangedSubview(_:)
insertArrangedSubview(_:atIndex:)
to reorder your views?
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