简体   繁体   中英

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

  • UIImageView
  • UIView
  • UIView

these are declared as open var in class code.

In my code I am adding another UIView like this:

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.

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

How can I bring the UIViews to front?

First clear the concept for bringSubviewToFront .

bringSubviewToFront can works only for direct subview or childview I mean you cannot do it for subview's subview.

For example you have view hierarchy like,

A -> B -> C

A is parent, B is it's subview and C is B's subview.

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,

 A?.bringSubview(toFront: B)

 B?.bringSubview(toFront: C)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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