[英]How can i find the superview/parentviewcontroller of a view inside a stackview
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. 我有一个主视图控制器(
MainView
),该视图的底部有一个stackview,而在stackview内部,我有三个视图。 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. 在其中一个视图中(我称它为
footerView
),我添加了一个小工具tooltipView
-并且由于footerView嵌套在MainView
底部的stackview内-它与其他视图有些重叠。 I want to tell the tooltipView
to be on top of all the views but it's not working. 我想告诉
tooltipView
在所有视图之上,但是它不起作用。 Inside footerView
- it has no superview
(I presume because it's inside a stackview) so I can't use parentView.bringSubviewToFront(childView)
. 在
footerView
内部-它没有superview
(我想是因为它在stackview内部),所以我不能使用parentView.bringSubviewToFront(childView)
。
What could I do instead? 我该怎么办呢? Is delegation through the layers the only way?
通过层级委派是唯一的方法吗?
You can manually set the zPosition
of each layer. 您可以手动设置每个图层的
zPosition
。 The default value is 0 so you may not get the desired value you want without manually controlling it. 默认值为0,因此如果不手动控制,可能无法获得所需的值。 As an example you could try
例如,您可以尝试
tooltipView.layer.zPosition = 1
tooltipView.layer.zPosition = 1
or 要么
tooltipView.layer.zPosition = .greatestFiniteMagnitude
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. 将其设置为1将使其高于其他默认设置为0的视图,或将其设置为
.greatestFiniteMagnitude
始终将其置于最前面。
Check out this answer on zPostion and Apple's documentation on zPostion . 在zPostion和Apple的zPostion文档中查看此答案 。
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
. 汤米的回答做了帮助,但另一件事,帮助是,我创造我
tooltipView
前footerView
已经完成正在建立-因此,为什么superview
是nil
。 I moved the initialisation of tooltipView
to be inside func layoutSubviews()
and it works. 我将
tooltipView
的初始化tooltipView
到func layoutSubviews()
内部,并且可以正常工作。
But ALSO make sure you untick clip to bounds
. 但是还请确保您取消勾画
clip to bounds
。 This is why no matter what i did with zPosition
made no difference 这就是为什么无论我对
zPosition
做什么都没有区别的原因
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.