[英]What is the correct way to get screen width/height with orientation changes?
I'm currently using [[UIScreen mainScreen] bounds].size.width
and height to get the screen width and height for getting a new image (that fills the screen) from a server.我目前正在使用
[[UIScreen mainScreen] bounds].size.width
和 height 来获取屏幕宽度和高度,以便从服务器获取新图像(填充屏幕)。 My problem is it appears that the bounds doesn't change on orientation changes.我的问题是,方向变化时边界似乎没有变化。 So if I rotate the device then it still gives me portrait width/heights even though the device is now in landscape.
因此,如果我旋转设备,即使设备现在处于横向状态,它仍然会给我纵向宽度/高度。 I'd like to not hardcode a screen width/height based on the current orientation.
我不想根据当前方向对屏幕宽度/高度进行硬编码。 Is there a way that I can check the screen width and height that will correctly reflect orientation changes?
有没有办法可以检查正确反映方向变化的屏幕宽度和高度?
Best way to do this is probably:最好的方法可能是:
CGSize appSize = [UIApplication sharedApplication].keyWindow.rootViewController.view.bounds.size;
and also use appSize.height
for height and appSize.width
for width This returns the orientation adjusted size of the topmost view which always fills the full screen, except possibly excluding the status bar depending on your status bar and wantsFullscreen settings.并且还使用
appSize.height
作为高度和appSize.width
作为宽度 这将返回始终填满全屏的最顶部视图的方向调整大小,但可能不包括状态栏,具体取决于您的状态栏和想要的全屏设置。
You can get the width and height by您可以通过
int h = self.view.frame.size.height;
int w = self.view.frame.size.width;
try get your height and width from applicationFrame尝试从 applicationFrame 获取您的高度和宽度
UIScreen.mainScreen().applicationFrame.size.height
UIScreen.mainScreen().applicationFrame.size.height
[UIScreen mainScreen].bounds.size
returns the bounds unrotated. [UIScreen mainScreen].bounds.size
返回未旋转的边界。 You can use the convertRect:fromView:
method (passing nil for the fromView) on an appropriate on-screen UIView to adjust for rotation and such.您可以在适当的屏幕 UIView 上使用
convertRect:fromView:
方法(为 fromView 传递 nil)来调整旋转等。
I've been working on an app where we have run into problems like this on a few occasions, so I hope I can help:我一直在开发一个应用程序,我们在几次遇到这样的问题,所以我希望我能提供帮助:
UIViewController
to manage your image.UIViewController
来管理您的图像。-shouldAutorotateToInterfaceOrientation:
on that view controller returns YES
.-shouldAutorotateToInterfaceOrientation:
在该视图上的实现 controller 返回YES
。UIImageView
, not a UIImage
for your picture, then try setting autoresizesSubviews
on the image view's superview (this is the view controller's view) to YES
.UIImageView
,而不是UIImage
的图片,然后尝试将图像视图的autoresizesSubviews
视图(这是视图控制器的视图)上的 autoresizedSubviews 设置为YES
。autoresizingMask
.autoresizingMask
。willRotateToInterfaceOrientation:duration:
and programmatically setting the image's frame.willRotateToInterfaceOrientation:duration:
并以编程方式设置图像的框架。 Remember that it's superview (the UIViewController's
view) hasn't rotated yet, so its frame hasn't updated.UIViewController's
视图)还没有旋转,所以它的框架还没有更新。willAnimateRotationToInterfaceOrientation:duration:
, though I'm not quite clear on the difference between this method and the former.willAnimateRotationToInterfaceOrientation:duration:
,尽管我不太清楚这种方法与前者之间的区别。 Hope this helps, and let me know your results!希望这会有所帮助,让我知道你的结果!
-Bruce -布鲁斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.