3

I got a Stack View with view: 1,2,3,4 and 5

Looking for a method to bring the Image View4 from view4 in the Stack view, above all the other views. The reason I need this is because I am moving the Image View4 over the Image View3 in view3.

I have tried to move view3 over view4 in the view hierarchy, but that just swaps their places in the Stack View.

enter image description here

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
Shift72
  • 426
  • 4
  • 21

3 Answers3

5

From the UIStackView documentation:

The order of [the views in] 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.

So since all views in the stack view's arrangedSubviews array are also in the stack view's subviews array, you should be able to use the standard UIView APIs to rearrange the views z-order:

  • bringSubview(toFront:)
  • sendSubview(toBack:)
  • exchangeSubview(at:withSubviewAt:)

I don't know whether this is possible to do in Interface Builder or not.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
1

Remove the image from the stack view and add it to the same view that contains the stack view as a subview, using addSubview(_:). That will put it on top of all other views. You could also use insertSubview(_:aboveSubview:) to insert it directly above the stack view.

You'll need to add constraints to the new view so that it is positioned where you want it.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

You can have IBOutlet property in UIViewController but in view hierarchy it is placed in UIStackView. So you should use

stackView.bringSubviewToFront(myView)

rather then

self.view.bringSubviewToFront(myView)

milczi
  • 7,064
  • 2
  • 27
  • 22