Changing frame of UIView's CALayer (self.view.layer.frame = …) appears to have no effect

让人想犯罪 __ 提交于 2019-12-05 06:46:08

Actually, the previous answer (you can't set uiview.layer.frame as it always fills the uiview) is close, but not quite complete. After reading the answer, I registered for the original site and to comment that the tutorial had issues. In doing so, I found that there were already comments that I hadn't seen in my first pass that addressed this. Using those, I started doing some testing.

The bottom line, if you move the self.view.layer.frame setting code from viewDidLoad to viewWillAppear, it works fine. That means that you can change the frame of the root layer of a view. However, if you do it in viewDidLoad it will be undone later.

However, the previous answer is still pretty close. I NSLog'ed the frame of the root layer and the frame of the view. Changing the root layer frame changes the view frame. So, the answer that the view.layer.frame always fills the view.frame is correct. However, setting the layer frame resets the view frame to match. (I'm guessing that uiview.frame property simply returns uiview.layer.frame...)

So, at some point in time between 2010 and today, something in the environment changed. Specifically, after viewDidLoad and before viewWillAppear the uiview/layer frame appears to be reset to the nib specified value. This overrides any changes in viewDidLoad. Changes made in viewWillAppear appear to stick.

Robin's answer got me on the right track, but I wanted to spell out the full answer.

The tutorial is wrong. Setting the frame of the view's main layer has no effect. The main layer is 'special' and will always fill the view's bounds. What you need to do is create a sublayer of the main layer like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CALayer *newLayer = [[CALayer alloc] init];

    newLayer.backgroundColor = [UIColor orangeColor].CGColor;
    newLayer.cornerRadius = 20.0;
    newLayer.frame = CGRectMake(100.0f, 100.0f, 200.0f, 200.0f);

    [self.view.layer addSublayer:newLayer];

    [newLayer release]; // Assuming you're not using ARC
}

Also, in your code a layer with width 20pt and height 20pt is too small to have rounded corners of 30pt anyway.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!