iOS:如何确定 viewWillAppear 中的当前方向?

发布于 2024-12-13 08:12:28 字数 86 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

依 靠 2024-12-20 08:12:28

使用 UIApplication 的 statusBarOrientation 属性。如果您使用设备方向UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown

示例

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation))
{
   // Do something when in landscape
}
else
{
   // Do something when in portrait
}

Use UIApplication's statusBarOrientation property. You may run into problems if you use the device orientation if it is UIDeviceOrientationFaceUp or UIDeviceOrientationFaceDown.

Example

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation))
{
   // Do something when in landscape
}
else
{
   // Do something when in portrait
}
怪我鬧 2024-12-20 08:12:28
UIDeviceOrientation getCurrentOrientation() {
  UIDevice *device = [UIDevice currentDevice];
  [device beginGeneratingDeviceOrientationNotifications];
  UIDeviceOrientation currentOrientation = device.orientation;
  [device endGeneratingDeviceOrientationNotifications];

  return currentOrientation;
}

您可以将 UIDeviceOrientation 转换为 UIInterfaceOrientation

UIDeviceOrientation getCurrentOrientation() {
  UIDevice *device = [UIDevice currentDevice];
  [device beginGeneratingDeviceOrientationNotifications];
  UIDeviceOrientation currentOrientation = device.orientation;
  [device endGeneratingDeviceOrientationNotifications];

  return currentOrientation;
}

It's up to you to convert the UIDeviceOrientation to UIInterfaceOrientation.

溺渁∝ 2024-12-20 08:12:28

当应用程序加载时,它不知道其当前方向 -

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait) {
    NSLog(@"portrait");// only works after a rotation, not on loading app
}

一旦旋转设备,您就会获得正确的方向,但是当加载应用程序时,在不更改方向的情况下,似乎使用 [[UIDevice currentDevice]orientation] 不知道当前方向。

因此,您需要做两件事 -

  1. 尝试在 plist 文件中设置应用程序接受的设备方向
  2. 在 UIViewControllers 中,您需要重写 shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) 方法,以便当应用程序应该 <代码>旋转:

When the app loads, it does not know its current orientation-

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait) {
    NSLog(@"portrait");// only works after a rotation, not on loading app
}

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 -

  1. Try setting the application's accepted device orientations in the plist file
  2. In your UIViewControllers, you will need to override the shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) method to return YES when the app should rotate:
最舍不得你 2024-12-20 08:12:28

如果相关调用的顺序意味着您不能依赖 interfaceOrientationviewWillAppear 处具有正确的值,那么 - 假设父视图控制器旋转 - 最安全的事情可能是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 at viewWillAppear then — assuming the parent view controller rotates — the safest thing is probably self.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文