问题
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/46780033/swift-bring-view-from-stack-view-to-front