2

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

potato
  • 4,479
  • 7
  • 42
  • 99
  • 1
    kindly add some screenshots, it would be easier to understand what you want to achieve – Usama Oct 08 '15 at 11:38
  • I edited the question. Seem like to much info is confusing – potato Oct 08 '15 at 11:39
  • 2
    In front of what? Because, normally developers try to bring subviews in front of _other_ subviews that are sharing a common parent... – Alladinian Oct 08 '15 at 11:40
  • 2
    you want to show only super view on screen and not its subview? – Usama Oct 08 '15 at 11:41
  • Why don't you just "trick" a subview to be as a "superView" graphically, and bring it as you want? – aramusss Oct 08 '15 at 11:43
  • Extactly, normally there is no need for such requirement. But if the superview is transparent you can add subview to it which should fill only transparent areas. – potato Oct 08 '15 at 11:43
  • 1
    @Usama83 I'm sorry. I didn't mean to be rude. And thanks for stopping by and trying to help me out. – potato Oct 08 '15 at 11:52

3 Answers3

2

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!

Abhinav
  • 37,684
  • 43
  • 191
  • 309
2

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.

Caleb
  • 124,013
  • 19
  • 183
  • 272
0

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.

Daniel T.
  • 32,821
  • 6
  • 50
  • 72