iPad - Getting screen size in portrait and landscape

邮差的信 提交于 2019-12-23 12:59:25

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!