Search code examples
iphoneiosuiviewcalayer

self.frame vs self.layer.bounds (in iOS)


I have a custom UIView set as my UIViewController's rootView. I start the application in landscape mode and I was having some issues regarding the bounds of my rootView. After some debugging I just used Xcode's console to check what was going on. The ITCustomView's class, is my UIViewController's rootView and it's a UIView's subclass. So I did:

po self

And the result:

ITCustomView: 0x15af30; frame = (20 0; 748 1024); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0x15b070>>

And then I did:

po [self layer]

And the result:

CALayer:0x15b070; position = CGPoint (394 512); bounds = CGRect (0 0; 1024 748); delegate = <ITCustomView: 0x15af30; frame = (20 0; 748 1024); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0x15b070>>; sublayers = (<CALayer: 0x15a390>, <CALayer: 0x15b2c0>, <CALayer: 0xce2f6a0>); transform = CATransform3D (0 -1 0 0; 1 0 0 0; 0 0 1 0; 0 0 0 1); backgroundColor = <CGColor 0x15b040> [<CGColorSpace 0x1356e0> (kCGColorSpaceDeviceRGB)] ( 0.818317 0.818317 0.818317 1 )

So my questions really are:

  1. Being in landscape shouldn't the frame be changed according to the orientation?

  2. Are these transformation (the orientation change) only "visible" on the layer?

  3. Should I start checking the bounds (to know when a UIView's subclass ends) using the self.layer.bounds instead of self.frame?

  4. Is is safer (aka: a best practise) to go always with the self.layer.bounds, because of possible transformations?

I find it odd (and also a bit ashamed of myself) to only have noticed this kind of behaviour now.


Solution

  • frame is only really relevant from the perspective of the superview, since it is expressed in the superview's coordinate system.

    For any internal purposes, the view's bounds rect should be used.

    So, for your sub-questions:

    1. No, check the bounds instead.
    2. No, check the bounds instead.
    3. No, check the bounds instead.
    4. No, check the bounds instead.

    You don't need to check the layer's bounds, you can use the view's bounds. So, your question should really be titled self.frame vs self.bounds, and bounds usually wins.