Add CALayer below UIView

20,529

Solution 1

[self.view.layer insertSublayer:CALayer below:subview.layer]

Solution 2

In Swift:

self.view.layer.insertSublayer(self.avPlayerLayer, below: self.button1.layer)

Solution 3

A more general solution where you don't have access to the subviews:

layer.insertSublayer(sublayer, at: 0)
Share:
20,529
DCMaxxx
Author by

DCMaxxx

Objective C lover. Interested in almost everything about computer sciences. Student at Epitech, currently studying abroad at RMIT, Australia

Updated on February 22, 2020

Comments

  • DCMaxxx
    DCMaxxx about 4 years

    I got a UIView (let's call it superview) which contains another UIView (lets call it subview).

    I'd like to add a CALayer so that it is visible on top of my superview, but under my subview. In other words, my subview's background should override the layer.

    I've had a look at addLayer:below: and others, but I'm not sure how to use it. Thanks.

    EDIT: My bad, I had another view between superview and subview, and I was messing with it. Thanks guys !

  • DCMaxxx
    DCMaxxx over 10 years
    My bad, I had another view between superview and subview, and I was messing with it. This works like a charm. Thanks !
  • Eugene P
    Eugene P over 10 years
    The answer is not correct enough. You do not need to insert you CALayer. Because if you add sublayer to your super view, this layer will definitely be under any subview of this superview.
  • Dima Deplov
    Dima Deplov almost 9 years
    what if we haven't self.button1.layer in self.view.layer.subalayers? If you know the answer please let me know
  • King-Wizard
    King-Wizard almost 9 years
    Let me know @flinth if my comment above helped you to fix your problem and do not forget to put a +1 for this answer.
  • Dima Deplov
    Dima Deplov almost 9 years
    unfortunately not. I know these methods. Look at my question please. stackoverflow.com/questions/30564635/…
  • King-Wizard
    King-Wizard almost 9 years
    @flinth I just answered your question.