Overlapping Views in UIStackView

nested uistackview
uistackview height based on content
uistackview constraints
uistackview not resizing
uistackview dynamic content
uistackview grid
uistackview hide subview
uistackview wrap content

I have an horizontal stack view that I added arranged sub views to it (7 cells). Each one of the cells in the stack has a circular badge that exceeds the view boundaries (negative constraint). When running, as you can see below, each cell is on top of the badge of the previous cell. I would like to change the order so that the badges will be fully visible.

Tried playing with the Z index but it didn't help as the layers are somehow not flat as you can see in the following view hierarchy:

Any idea or suggestion how to do it?

Thanks.

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 :-)

UIStackView, Many of us reserve stack views for only the most mundane layouts in iOS. Introduced in iOS 9, UIStackView is the most recent addition to the UI control negative spacing values cause the subviews to overlap and the last  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.

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!

spacing, The distance in points between the adjacent edges of the stack view's arranged views. Availability This property defines a strict spacing between arranged views for the UIStackView.Distribution. Use negative values to allow overlap. If the views overlap, subviews with a lower index appear behind subviews with a higher index. Dynamically Changing the Stack View’s Content. The stack view automatically updates its layout whenever views are added, removed or inserted into the arranged Subviews array, or whenever one of the arranged subviews’s is Hidden property changes.

Since iOS 9.0 it's possible to set the z-order and arrangement of stack view subviews independently. Use the new arrangedSubviews property to specify the subviews arrangement along the stack view’s axis. And use subviews property to specify subviews z-order based on their index in the array.

The stack view ensures that its arrangedSubviews property is always a subset of its subviews property.

Documentation for arrangedSubviews

UIStackView, The stack view manages the layout of all the views in its arrangedSubviews If the views overlap, subviews with a lower index appear behind subviews with a  With fill distributions, negative spacing values cause the subviews to overlap and the last subview to stretch, filling the freed up space. Negative spacing values have no effect on equal centering or spacing distributions. plate Stack. spacing = 2 // These plates can float too! The spacing property applies equally between each pair of arranged subviews.

An other idea could be to set background color of the subviews (7 cells) to default (fully transparent). And only set the background of the stack view itself to white.

I have a similar issue but where every subview has to have a specific background. And in my case, your hack might help… 😉 Thanks for it!

Common issues of UIStackView, Learn how to simplify your iOS layouts with UIStackView. Layout a series of views horizontally or vertically, using alignment, distribution and  Fortunately, embedding existing views into a new stack view is a piece of cake. First, select all the buttons at the bottom of the Spot Info View Controller scene by clicking on one, then command-clicking on the other two. Open the outline view by using the Show Document Outline button at the bottom-left of the storyboard canvas.

Below is a simple extension on UIStackView to reverse the z-index of the UIStackView's subviews and optionally require layout:

extension UIStackView {

  func reverseSubviewsZIndex(setNeedsLayout: Bool = true) {
    let stackedViews = self.arrangedSubviews

    stackedViews.forEach {
      self.removeArrangedSubview($0)
      $0.removeFromSuperview()
    }

    stackedViews.reversed().forEach(addSubview(_:))
    stackedViews.forEach(addArrangedSubview(_:))

    if setNeedsLayout {
      stackedViews.forEach { $0.setNeedsLayout() }
    }
  }

}

UIStackView Tutorial for iOS: Introducing Stack Views , Overlapping Views in UIStackView. Question. I have an horizontal stack view that I added arranged sub views to it (7 cells). Each one of the cells in the stack has  Stack views are easily used within a scroll view: let stackView = UIStackView() scrollView.addSubview(stackView) //A little helper I use to set top/bottom/leading/trailing constraints to another

Overlapping Views in UIStackView, Just out of curiosity if you're using a stack view, why are you also manually setting frame in layoutSubviews? Another thing to consider is the "  A layout where the stack view resizes its arranged views so that they fill the available space along the stack view’s axis. The views are resized so that they are all the same size along the stack view’s axis.

Xamarin IOS overlapping subviews in UIStackView, To summarize the bright side features of UIStackView: layout and arranged subviews positioning; can be stacked inside another stack view  When the stack view distribution property is set to FillProportionally the spacing value dictates the spacing between the subviews. In EqualSpacing and EqualCentering modes, the spacing value indicates the minimum allowed spacing between the adjacent edges of the subviews. A negative spacing value causes subviews to overlap.

UIStackView: Lessons learned - INLOOPX, My problem – when I add arranged subviews to a stackview programmatically, they just pile up in the upper right-hand corner as shown below. UIStackView is very powerful layout component but it can cause a lot of issues. In this article I will run through some of the common mistakes that we all tend to do while using UIStackView. If…

Comments
  • for a quick example of changing the view z order but not the arranged order, you can do something like this stackView.insertSubview(stackView.arrangedSubviews[0], at: 1) which will swap the 1st and 2nd items draw order but not their displayed order
  • It looks like it will insert a duplicatedView in your subviews to me