简体   繁体   English

通过方向更改获取屏幕宽度/高度的正确方法是什么?

[英]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:我一直在开发一个应用程序,我们在几次遇到这样的问题,所以我希望我能提供帮助:

  1. First off, make sure you are using a UIViewController to manage your image.首先,确保您使用的是UIViewController来管理您的图像。
  2. Secondly, make sure your implementation of -shouldAutorotateToInterfaceOrientation: on that view controller returns YES .其次,确保您的-shouldAutorotateToInterfaceOrientation:在该视图上的实现 controller 返回YES
  3. Make sure you are using a 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
  4. If the image resizes, but not to your liking, try messing with the image view's autoresizingMask .如果图像调整大小,但不是您喜欢的,请尝试弄乱图像视图的autoresizingMask
  5. If, after step 3, the image doesn't autoresize, try implementing willRotateToInterfaceOrientation:duration: and programmatically setting the image's frame.如果在第 3 步之后图像没有自动调整大小,请尝试实现willRotateToInterfaceOrientation:duration:并以编程方式设置图像的框架。 Remember that it's superview (the UIViewController's view) hasn't rotated yet, so its frame hasn't updated.请记住,它的超级视图( UIViewController's视图)还没有旋转,所以它的框架还没有更新。
  6. Finally, if that doesn't work, try 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.

相关问题 TTStyledTextLabel如何在方向更改时更改宽度/高度? - How can TTStyledTextLabel change width/height on orientation changes? iOS-反转方向的高度/宽度 - iOS - reversed height/width with orientation 从iPhone中的主屏幕图标启动时,方向更改未在PWA中提供适当的窗口高度和宽度 - Orientation change is not giving proper window height and width in PWA when launched from home screen icon in iPhone UIImageView在旋转时会改变其宽度和高度,如何获得原始高度和宽度的正常状态? - UIImageView on rotating changes it width and height, how to get original height and width of normal state? UIView将方向切换为横向后会更改宽度吗? - UIView changes width after switching orientation to landscape? 启动画面图像的方向不正确 - Splash Screen Image's orientation is not correct 获取设置视图当前方向的最佳方法是什么 - What is the best way to get the current orientation for setting views 当我改变方向时,我的视图的高度和宽度不会改变 - When i changing orientation my view's height and width are not changed 为什么iPhone 4、5、6的横向宽度和高度高度紧凑 - Why compact width and compact height in iPhone 4,5,6 in landscape orientation iPhone:如何动态设置UIImage的大小,宽度,高度和方向? - iPhone:How to dynamically set UIImage size , width , height and orientation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM