问题
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.
回答1:
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.
回答2:
Ok. In that case we can use,
CGRect viewFrame = self.view.frame; int width = viewFrame.size.width; int height = viewFrame.size.height;
回答3:
CGSize winSize = [[CCDirector sharedDirector] winSize];
winSize.width will give you width and winSize.height give you height.
来源:https://stackoverflow.com/questions/5603690/ipad-getting-screen-size-in-portrait-and-landscape