【问题标题】:Forcing UIInterfaceOrientation changes on iPhone在 iPhone 上强制 UIInterfaceOrientation 更改
【发布时间】:2011-02-10 23:42:38
【问题描述】:

我正在努力获得一个 iPhone 应用程序,该应用程序几乎需要导航控制器堆栈中的每次推送或弹出来改变方向。

基本上第一个视图是纵向的,第二个是横向的,第三个是纵向的(是的,我知道这不太理想,但这就是设计,我必须实现它。

我在这里得到了各种建议....
How do I detect a rotation on the iPhone without the device autorotating?
Force portrait orientation on pushing new view to UINavigationViewController
Is there a documented way to set the iPhone orientation?
但没有完全成功。

设置链接到 3.1.2 我对上面链接文章的阅读似乎表明,如果我的纵向视图推送视图

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return ((interfaceOrientation == UIInterfaceOrientationLandscapeRight) );
}
然后该视图应显示为旋转为横向。发生的情况是它以“破碎”的肖像形式出现,然后在设备转动时正确旋转。

如果我将控制器弹出回我的纵向视图(其中有一个适当的 shouldAutoRotate...),那么它将保持在损坏的横向视图中,直到设备返回纵向。

我还尝试删除所有 shouldautorotate 消息,而是通过转换视图来强制旋转。这种方法有效,我发现通过移动状态栏(实际上隐藏在我的应用程序中)[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;,键盘会在需要时以正确的方向显示。

这种方法的问题在于,当您没有状态栏时,状态栏转换会变得奇怪而难看 - 每次更改都会在页面上隐约出现阴影。

所以。我错过了什么。

1) 我认为在 3.1.2(或可能更早的版本)中 shouldAutorotateToInterfaceOrientation 应该通过推动控制器来提供所需的方向,我错了吗?

2) 是否有另一种方法可以让键盘以正确的方向显示。

3) 未记录的 API 调用是否可行(请不要!)

【问题讨论】:

    标签: iphone objective-c uiinterfaceorientation


    【解决方案1】:

    您不应该使用[UIViewController shouldAutorotateToInterfaceOrientation:] 来触发方向更改;它只是让系统知道是否允许自动旋转。您仍然应该更新它以指定允许的方向。

    如果您想在显示特定视图时更改方向,您应该在您的 [UIViewController viewWillAppear:] 覆盖方法中为每个强制特定方向的视图控制器调用 [UIApplication setStatusBarOrientation:animated:]。当视图被推入堆栈以及从堆栈中弹出时,这将导致更改。确保在覆盖方法中调用 super

    如果您正在这样做,这也是更改状态栏显示方式的正确位置。

    【讨论】:

    • 我不确定 [UIApplication setStatusBarOreintation.... 和 [UIApplication sharedApplication].statusBarOrientation... 之间是否有区别...我会测试,否则这就是我所做的,并且我正在遭受状态栏旋转的阴影。在横向视图中,我最初使用转换以横向显示视图。如果我然后允许使用 shouldAutorotateToInterfaceOrientation 进行横向视图,它不会再次转动吗?我该如何覆盖它?
    • 请注意,此方法现已在 iOS 9 中弃用。
    猜你喜欢
    • 2012-02-02
    • 1970-01-01
    • 2011-04-22
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多