Satyam
Satyam

Reputation: 15894

iPad - Getting screen size in portrait and landscape

I'm using the following code to get the screen size width:

CGFloat width = [UIScreen mainScreen].bounds.size.width - 100;

But its giving width as "668.0" in portrait as well as landscape. How can I get different width depending on the orientation of the device.

Upvotes: 4

Views: 4151

Answers (3)

Johann
Johann

Reputation: 12398

I ran into the same problem as you and all I could find is checking the orientation and calculate the height and width as follow:

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
screenHeight = orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight ? 748 : 1004;
screenWidth = orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight ? 1024 : 768;

These size are for the the iPad obvioulsy but it could work for iPhone with the right dimensions.

Personally I think it's a bit poor but I couldn't find anything else.

Upvotes: 3

ShinuShajahan
ShinuShajahan

Reputation: 1296

Ok. In that case we can use,

CGRect viewFrame = self.view.frame;
int width = viewFrame.size.width;
int height = viewFrame.size.height;

Upvotes: 3

ShinuShajahan
ShinuShajahan

Reputation: 1296

CGSize winSize = [[CCDirector sharedDirector] winSize];

winSize.width will give you width and winSize.height give you height.

Upvotes: 1

Related Questions