简体   繁体   English

在自定义UIVIew中将子视图置于最前面

[英]Bringing Subview to front in custom UIVIew

I am using a custom UIView which contains the following elements by default (after init is called these are set) : 我使用的是自定义UIView ,默认情况下包含以下元素(在调用init之后,将其设置):

  • UIImageView
  • UIView
  • UIView

these are declared as open var in class code. 这些在类代码中声明为open var

In my code I am adding another UIView like this: 在我的代码中,我添加了另一个类似的UIView

myView?.addSubview(div)
myView?.bringSubview(toFront: div)

Now calling bringSubviewToFront works only on the div element which was added by code and not on the default views. 现在,调用bringSubviewToFront仅适用于通过代码添加的div元素,而不适用于默认视图。

myView?.bringSubview(toFront: myView?.likedView)

How can I bring the UIViews to front? 如何将UIView放在最前面?

First clear the concept for bringSubviewToFront . 首先,要清除bringSubviewToFront的概念。

bringSubviewToFront can works only for direct subview or childview I mean you cannot do it for subview's subview. bringSubviewToFront只能用于直接子视图或子视图,我的意思是您不能用于子视图的子视图。

For example you have view hierarchy like, 例如,您具有如下视图层次结构,

A -> B -> C A-> B-> C

A is parent, B is it's subview and C is B's subview. A是父级,B是它的子视图,C是B的子视图。

Now you can directly do like 现在您可以直接做喜欢

 A?.bringSubview(toFront: B)

but can't do like, 但是做不到

 A?.bringSubview(toFront: C)

If you want C to bring in front then you have to do like, 如果您想让C领先,那么您必须喜欢

 A?.bringSubview(toFront: B)

 B?.bringSubview(toFront: C)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM