Overlapping Views in UIStackView
This Swift solution reverses the stack view subview order using the UIView method sendSubviewToBack(_:)
(docs here):
@IBOutlet weak var stackView: UIStackView!
for view in stackView.arrangedSubviews {
stackView.sendSubviewToBack(view)
}
As we advance through the stack view's arranged views (from bottom to top), the later (i.e. higher) views get pushed to the bottom, thereby reversing the order :-)
Something for 2022:
Even Swiftier...
@IBOutlet weak var stackView: UIStackView!
stackView.arrangedSubviews.forEach({ stackView.sendSubviewToBack($0) })
The documentation for the UIStackView class tells that:
The order of the subviews array defines the Z-order of the subviews. If the views overlap, subviews with a lower index appear behind subviews with a higher index.
which is exactly what I experienced in the question. To over come my specific case I did the trick of changing the semantic of the view to be RTL as can bee seen here:
This is not covering the a generic case because a device may have been set a RTL language in its global settings. For the generic case I guess I will need to check the interface semantic and force an opposite direction to my stack view.
P.S. It's kind of hack so any ideas about how to reorder the Z-order of subviews in a different way will be most welcome!