UIDevice currentDevice 的“方向”始终为空

发布于 2024-08-25 06:32:30 字数 317 浏览 16 评论 0原文

正如标题所示。 调用 [[UIDevice currentDevice] BeginGenerateDeviceOrientationNotifications] 没有任何效果。

DidRotateToInterfaceOrientation 等事件工作正常,但我需要能够任意轮询设备方向。

我该如何修复/执行此操作?

长话短说: 我有一个选项卡应用程序,每个选项卡上都有一个导航控制器。第一个选项卡的根视图是一个图表,当方向更改为横向时,该图表会全屏显示;然而,每当视图出现时都需要检查这一点,因为方向变化可能发生在其他地方,所以我希望每当此视图出现时轮询方向状态。

As per the title.
Calling [[UIDevice currentDevice] BeginGeneratingDeviceOrientationNotifications] has no effect.

DidRotateToInterfaceOrientation etc events are working fine, but I need to be able to poll the device orientation arbitrarily.

How can I fix/do this?

The long story:
I have a tab application with a navigation controller on each tab. The root view of tab number one is a graph that goes full screen when the orientation changes to landscape; however this needs to be checked whenever the view appears as the orientation change could have occurred elsewhere, so I was hoping to poll the orientation state whenever this view appears.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

酒儿 2024-09-01 06:32:30

UIDevice 的方向概念似乎仅适用于实际设备。无论是否按照文档建议启用了通知,模拟器似乎总是在此处返回 0。非常不方便,但就这样吧。

我发现这在实际设备上运行良好:

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    NSLog(@"orientation: %d", [[UIDevice currentDevice] orientation]);
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

UIDevice's notion of orientation seems to be only available on actual devices. The simulator seems to always return 0 here, regardless of whether the notifications have been enabled as the docs suggest. Irritatingly inconvenient, but there you go.

I find this works fine on the actual device:

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    NSLog(@"orientation: %d", [[UIDevice currentDevice] orientation]);
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
终难遇 2024-09-01 06:32:30

似乎是一个愚蠢的问题,但不是吗

开始生成设备方向通知

(小写 b )
...

seems like a silly question, but isn't it

beginGeneratingDeviceOrientationNotifications

( lower case b )
...

柠檬心 2024-09-01 06:32:30

如果您在 - (void)viewDidLoad 中检查 [[UIDevice currentDevice]orientation] ,您将始终得到 nil。

在 *- (void) viewDidAppear:(BOOL)* 动画方法中检查它,然后您

If you check [[UIDevice currentDevice] orientation] in - (void)viewDidLoad you will always get nil.

Check it in *- (void) viewDidAppear:(BOOL)*animated method and you

云雾 2024-09-01 06:32:30

这是按照 iMeMyself 在上面的评论中所说的 - 这让我花了很多时间,我认为这是正确的答案,所以我想在这里强调它:

UIDeviceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
   //do stuff here
}
else if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
{
//or do stuff here
}

this is as per iMeMyself said in the comments above - this samed me a lot of time and I think is the right answer so I wanted to highlight it here:

UIDeviceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
   //do stuff here
}
else if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
{
//or do stuff here
}
沙沙粒小 2024-09-01 06:32:30

[[UIDevice currentDevice]orientation] 不会给你当前的方向状态吗?您可以在想要轮询的视图控制器的 viewWillAppear 方法中检查这一点。

编辑:除此之外,还有多种方法可以获取当前方向,例如使用 UIApplication 中的 statusBarOrientation 属性,或 UIViewcontroller 中的 interfaceOrientation 属性。

Wouldn't [[UIDevice currentDevice] orientation] give you the current orientation state? You could check for this in your viewWillAppear method of the view controller that wants to poll.

Edit: Other than that, there are various ways to get the current orientation, such as using the statusBarOrientation property in UIApplication, or interfaceOrientation property in UIViewcontroller.

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