【问题标题】:How to support only portrait mode on an iPhone app如何在 iPhone 应用程序上仅支持纵向模式
【发布时间】:2023-04-01 18:44:01
【问题描述】:

我正在开发的 iPhone 应用程序中遇到一个奇怪的问题。我希望我的应用支持ONLY纵向模式,但由于某种原因我不能做到(设备和模拟器)。

为了只支持纵向模式,我做了如下操作:

  • 在 Xcode 的 TARGET 摘要部分,我只选择了纵向。
  • 我所有的 ViewController 都实现了 shouldAutorotateToInterfaceOrientation

但正如我所说,它不起作用,奇怪的结果是应用支持所有方向(纵向、倒置、横向左侧、横向右侧)。
有任何想法吗?

这是我如何实现shouldAutorotateToInterfaceOrientation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
     // Return YES for supported orientations
     NSLog(@"Checking orientation %d", interfaceOrientation);
     return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

我刚刚注意到,当我转动手机时,我收到了这样一条消息:

“两阶段旋转动画已弃用。此应用程序应 使用更流畅的单阶段动画。”

什么意思?

【问题讨论】:

  • 试试我的答案。也许会有所帮助。
  • 尝试将NSLog(@"Checking orientation %d", interfaceOrientation); 添加到您的shouldAutorotateToInterfaceOrientation 方法中。当您旋转模拟器或您的设备时,是否会调用它们中的任何一个?
  • 感谢您的帮助,是的,日志打印“检查方向 1”。但我注意到了其他问题,请检查我更新的问题
  • @Eyal:额外的控制台消息可能意味着您的代码实现了旧的和已弃用的didAnimateFirstHalfOfRotationToInterfaceOrientation:,以及类似的 willAnimateFirstHalfOfRotationToInterfaceOrientation:duration: 和 willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:。你也许可以不用这些方法。
  • @Eyal: interfaceOrientation of 1 是UIInterfaceOrientationPortrait,这是你所期望的。如果当您旋转到横向时确实发生了这种情况,这比以往任何时候都更奇怪:设备返回 YES 因为它认为它正在旋转到纵向。可以发一个完整的shouldAutorotateToInterfaceOrientation方法吗?

标签: iphone objective-c xcode autorotate device-orientation


【解决方案1】:

在目标摘要中仅选择纵向。

【讨论】:

  • 尝试删除 shouldAutorotateToInterfaceOrientation 方法并保持目标设置正确。
【解决方案2】:

转到 info.plist 文件。右键单击将其作为源代码打开。并寻找这条线。对我来说 iPad 是这样的:

  <key>UISupportedInterfaceOrientations~ipad</key>

删除所有其他方向并保留您唯一需要的方向..像这样:

    <array>

    <string> UIInterfaceOrientationPortrait </string>

</array>

【讨论】:

    【解决方案3】:

    屏幕上可以有多个 ViewController。 UITabBarController 本身就是一个UIViewController,如果它选择,它只会将shouldAutorotateToInterfaceOrientation: 请求传递给其中的viewControllers。默认实现会执行此操作,但如果您将其子类化,则 XCode 生成的代码(从 iOS 5.1 开始)不会。

    【讨论】:

      【解决方案4】:

      检查您的 plist 并确保那里的密钥设置正确。

      【讨论】: