VJVJ
VJVJ

Reputation: 443

Why self.view.frame.size.width is showing 768 in iPad landscape mode

I am creating an app for iPad landscape mode. I have created a UIView for entire screen. But it is not displaying entire screen as width of 1024. It shows for the width of 768(means 0 to 768).

backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[backgroundView setBackgroundColor:[[UIColor greenColor]colorWithAlphaComponent:0.5]];
backgroundView.opaque = NO;
backgroundView.userInteractionEnabled = YES;
backgroundView.clearsContextBeforeDrawing=YES;
[self.view addSubview:backgroundView]; 

CGFloat x = self.view.frame.size.width;
NSLog(@"size%f",x); // 768

It happens when i run in iPad 2 (7.1) simulator

Please advice.

Upvotes: 2

Views: 1484

Answers (2)

Mihir Mehta
Mihir Mehta

Reputation: 13833

Did you write this code in viewdidload ?

If yes than viewDidLoad will not have the right width and height. You need to write it in viewDidLayoutSubviews method.

like

-(void)viewDidLayoutSubviews {
    if(backgroundView == nil) {
    backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[backgroundView setBackgroundColor:[[UIColor greenColor]colorWithAlphaComponent:0.5]];
backgroundView.opaque = NO;
backgroundView.userInteractionEnabled = YES;
backgroundView.clearsContextBeforeDrawing=YES;
[self.view addSubview:backgroundView]; 

CGFloat x = self.view.frame.size.width;
NSLog(@"size%f",x); // 768
}
}

Upvotes: 3

Peter Segerblom
Peter Segerblom

Reputation: 2813

I remember apple changed how orientation is handled between two versions of iOS, probably 7 and 8 then. In 7 width is always width of device disregarding what orientation it is in while in 8 it takes in to account the orientation. So in short its the "height" you should use for "width" in lanscape mode iOS7.

Upvotes: 2

Related Questions