potato
potato

Reputation: 4599

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(_:)

Upvotes: 2

Views: 1019

Answers (3)

Daniel T.
Daniel T.

Reputation: 33979

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.

Upvotes: 0

Caleb
Caleb

Reputation: 125037

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.

Upvotes: 2

Abhinav
Abhinav

Reputation: 38162

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!

Upvotes: 2

Related Questions