iOS:如何确定 viewWillAppear 中的当前方向?
我有一个 UIWebView,我想根据它的纵向还是横向加载不同的 URL。如何确定我当前在 viewWillAppear
内的方向?
I have a UIWebView
that I would like to load different URLs depending on whether its portrait or landscape. How can I figure out what my current orientation is inside viewWillAppear
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 UIApplication 的
statusBarOrientation
属性。如果您使用设备方向UIDeviceOrientationFaceUp
或UIDeviceOrientationFaceDown
。示例
Use UIApplication's
statusBarOrientation
property. You may run into problems if you use the device orientation if it isUIDeviceOrientationFaceUp
orUIDeviceOrientationFaceDown
.Example
您可以将
UIDeviceOrientation
转换为UIInterfaceOrientation
。It's up to you to convert the
UIDeviceOrientation
toUIInterfaceOrientation
.当应用程序加载时,它不知道其当前方向 -
一旦旋转设备,您就会获得正确的方向,但是当加载应用程序时,在不更改方向的情况下,似乎使用
[[UIDevice currentDevice]orientation]
不知道当前方向。因此,您需要做两件事 -
shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)
方法,以便当应用程序应该 <代码>旋转:When the app loads, it does not know its current orientation-
Once you rotate the device, you get correct orientation, but when the app is loaded, without changing the orientation, it seems that using
[[UIDevice currentDevice] orientation]
doesn't know the current orientation.So you need to do 2 things -
shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)
method to return YES when the app shouldrotate:
如果相关调用的顺序意味着您不能依赖
interfaceOrientation
在viewWillAppear
处具有正确的值,那么 - 假设父视图控制器旋转 - 最安全的事情可能是self.parentViewController.interfaceOrientation
。如果失败,您可以尝试从[[UIDevice currentDevice]orientation]
做出第一个假设,这可能并不总是 100% 省钱(例如,如果当您的应用程序运行时设备平放在桌子上)推出),但可能比总是假设肖像给你更好的猜测。If the is sequencing of relevant calls mean that you can't rely on
interfaceOrientation
having the correct value atviewWillAppear
then — assuming the parent view controller rotates — the safest thing is probablyself.parentViewController.interfaceOrientation
. Failing that you could try making a first assumption from[[UIDevice currentDevice] orientation]
, which may not always be 100% on the money (e.g. if the device is lying flat on a table when your app is launched) but is likely to give you a better guess than always assuming portrait.