简体   繁体   中英

Change UIView display order globally within view hierarchy

Consider the following view hierarchy:

let bottomView = UIView()
addSubview(bottomView)

let bottomViewSubview = UIView()
bottomView.addSubview(bottomViewSubview)

let topView = UIView()
addSubview(topView)

// bottomViewSubview.displayOnTopOfTopView() - How?

I'm wondering if there is a possibility in UIKit without changing the view/subview hierarchy, to always display a subview of some view on top of all other views, including views that cover its superview. As far as I know CALayer.zIndex works only within UIView's sublayers, not globally as I need. And I can't change the hierarchy since I want to rely on it in everything else like subviews transforms.

I guess the only option is to create the new view, which is actually on top of the view hierarchy, and manually sync all the transforms for it to appear like it's someone's subview?

You could remove the view and use the insert(_: at:) function to move the views. Alternatively, bringSubviewToFront(UIView) and sendSubviewToBack(UIView) can be used. If you don't have a reference to the view you need to move, you can use the subviews property to find it.

bringSubviewToFront(bottomViewSubview)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM