0

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?

JVS
  • 2,592
  • 3
  • 19
  • 31
  • 1
    Something is wrong with your code you have shown here. `bringSubView` does not accept optional value (view) . `open func bringSubview(toFront view: UIView)` – Krunal Oct 31 '17 at 12:46
  • is likedView initilized? – Baig Oct 31 '17 at 12:50
  • Have you made class for your myView & how is it added to your controller? Check if its child views are properly initilized. – Baig Oct 31 '17 at 12:52

1 Answers1

3

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)
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75