iPad - 如何在竖屏和横屏模式下获取屏幕尺寸

4
我使用以下代码获取屏幕宽度大小:
CGFloat width = [UIScreen mainScreen].bounds.size.width - 100;

但它在横向和纵向时都给出宽度为"668.0"。我该如何根据设备的方向获得不同的宽度。

3个回答

3

我遇到了和你相同的问题,所以我只能检查方向并按以下方式计算高度和宽度:

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

这些尺寸明显是针对iPad的,但如果有正确的尺寸,它也可以适用于iPhone。个人认为它有点简陋,但我找不到别的东西。

你可以使用 UIInterfaceOrientationIsPortrait(orientation)UIInterfaceOrientationIsLandscape(orientation) 代替 orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight 或者 orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight - P. Unto

3

好的。在这种情况下,我们可以使用以下代码:

CGRect viewFrame = self.view.frame;
int width = viewFrame.size.width;
int height = viewFrame.size.height;
该代码段涉及视图框架和宽度、高度的获取。

我的类继承自NSObject。没有办法使用view,所以我尝试使用UIScreen。也许我需要尝试其他的东西。 - Satyam

1
CGSize winSize = [[CCDirector sharedDirector] winSize];

使用winSize.width可获取宽度,使用winSize.height可获取高度。


我没有使用cocos2d。在iPhone原生SDK中是否有类似的方法(不是在cocos2d或其他什么地方)? - Satyam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接