简体   繁体   中英

how does one bring superview to front?

Normally developers try to bring subview to front. On contrary, how does one bring superview to front?

I'm looking for a reverse of bringSubviewToFront(_:) . It should look like bringSuperviewToFront(_:)

I do not think you can do that. Views are laid down in layers with each sub view layer being part of super view layer. The effect you want to materialise can be achieved by hiding/removing all the sub views of the view you want to get Bring To Front effect on.

This works well with sub views because normally you would want to show/hide subviews inside same parent view based on their layer position!

I'm looking for a reverse of bringSubviewToFront( :). It should look like bringSuperviewToFront( :)

That doesn't exist. Instead of giving your button a subview, make both the subview and the button subviews of some container. Then you can adjust their relative z positions to suit your purpose.

In the spirit of being less rude.

func bringSubviewToFront(view: UIView) {
    let superview = view.superview
    superview?.addSubview(view)
}

That will bring it to the front. As in:

bringSubviewToFront(mySubview)

What the above code does is detach the view from wherever it is in its superview's hierarchy and reattach it to the top of all the subviews. This has the effect of bringing it to the front like you want. If the view isn't part of a hierarchy, then the function does nothing.

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