Swift: Bring View from Stack View to front

别来无恙 提交于 2019-12-06 12:11:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!