Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 774
Anthony Kong Avatar asked May 22 '11 05:05

Anthony Kong


1 Answers

setContentsScale has no effect

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

However, if I use

  [ca setFrame:self.view.bounds];

it works.

like image 92
Anthony Kong Avatar answered Sep 24 '22 07:09

Anthony Kong