How can i find the superview/parentviewcontroller of a view inside a stackview

133 Views Asked by At

Hopefully this isn't too confusing.

I have a main view controller (MainView) with a stackview at the bottom of the view and inside the stackview I have three views. Inside one the views (lets call it footerView) i'm adding a little tooltipView - and because footerView is nestled inside a stackview at the bottom of MainView - it's got some overlap with the other views. I want to tell the tooltipView to be on top of all the views but it's not working. Inside footerView - it has no superview (I presume because it's inside a stackview) so I can't use parentView.bringSubviewToFront(childView).

What could I do instead? Is delegation through the layers the only way?

2

There are 2 best solutions below

0
Tommy On

You can manually set the zPosition of each layer. The default value is 0 so you may not get the desired value you want without manually controlling it. As an example you could try

tooltipView.layer.zPosition = 1

or

tooltipView.layer.zPosition = .greatestFiniteMagnitude

Setting it to 1 will make it above other views that default to 0 or setting it to .greatestFiniteMagnitude will always have it at the front.

Check out this answer on zPostion and Apple's documentation on zPostion.

0
vanilla_splsh On

Tommy's answer did help but another thing that helped was that I was creating my tooltipView before footerView had finished being set up - hence why the superview was nil. I moved the initialisation of tooltipView to be inside func layoutSubviews() and it works.

But ALSO make sure you untick clip to bounds. This is why no matter what i did with zPosition made no difference