我使用以下代码获取屏幕宽度大小:
CGFloat width = [UIScreen mainScreen].bounds.size.width - 100;
但它在横向和纵向时都给出宽度为"668.0"。我该如何根据设备的方向获得不同的宽度。
我遇到了和你相同的问题,所以我只能检查方向并按以下方式计算高度和宽度:
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
screenHeight = orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight ? 748 : 1004;
screenWidth = orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight ? 1024 : 768;
好的。在这种情况下,我们可以使用以下代码:
CGRect viewFrame = self.view.frame; int width = viewFrame.size.width; int height = viewFrame.size.height;该代码段涉及视图框架和宽度、高度的获取。
CGSize winSize = [[CCDirector sharedDirector] winSize];
使用winSize.width可获取宽度,使用winSize.height可获取高度。
UIInterfaceOrientationIsPortrait(orientation)
和UIInterfaceOrientationIsLandscape(orientation)
代替orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight
或者orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight
。 - P. Unto