Search code examples
iosuiviewuiviewcontrollercalayeriphone-4

iOS: self.view.bounds does not fill the whole window


I am doing some simple testing of adding CALayer to a UIView. In my main controller class of an iPhone 4 app, I implemented the viewDidLoad method as such:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"%s", __PRETTY_FUNCTION__);

    CALayer* ca = [[CALayer alloc] init];
    [ca setBounds:self.view.bounds];

    [ca setBackgroundColor:[[UIColor blueColor] CGColor]];

    [self.view.layer addSublayer:ca];
}

The blue background only occupy 1/4 of the screen.

enter image description here

I wonder if it is because I did not take retina display into consideration? What is the best practice in this situation?


Added these debug messages:

NSLog(@"frame w:%f h:%f", self.view.frame.size.width, self.view.frame.size.height);
NSLog(@"bounds w:%f h:%f", self.view.bounds.size.width, self.view.bounds.size.height);

Output:

frame w:320.000000 h:460.000000
bounds w:320.000000 h:460.000000

Solution

  • setContentsScale has no effect

      [ca setContentsScale:[[UIScreen mainScreen] scale]];
    

    However, if I use

      [ca setFrame:self.view.bounds];
    

    it works.