JVS
JVS

Reputation: 2682

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

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?

Upvotes: 0

Views: 2361

Answers (1)

Ketan Parmar
Ketan Parmar

Reputation: 27448

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)

Upvotes: 3

Related Questions