small medium large xlarge

Generic-user-small
16 Jun 2009, 04:01
Bryan Hughes (8 posts)

I have seen several examples from several books. In some examples they have used the following code pattern:

	[self.layer addSublayer:myView.layer];	

In others, I have seen

	[self addSubview:myView];

What is the difference and why would another author use the first pattern?

Thanks…

Generic-user-small
17 Aug 2009, 06:50
Darren Minifie (2 posts)

Hey Bryan

The two examples you present are not equivalent. The first is manipulating the layer hierarchy, and the second is modifying the view hierarchy. Remember views have much more going on than layers (all the responder stuff, etc). Hopefully this makes some sense.

Biopic_100x100_pragsmall
17 Aug 2009, 13:57
Bill Dudney (917 posts)

Hi Bryan,

To Darren’s comments I’d add…

In most circumstances the first thing ([self.layer addSublayer:myView.layer]) is a very bad idea regardless of it working or not.

A view typically owns its layer and the layer should not be manipulated apart from its view. If you have taken control and placed your own layer into a view and don’t need the view any more, remove the layer from that view before adding it to a new layer hierarchy. You will save yourself much headache in strange unexpected behavior.

Good luck!

You must be logged in to comment