【问题标题】:UIViewController interfaceOrientation problemUIViewController 界面方向问题
【发布时间】:2023-10-19 19:07:01
【问题描述】:

像往常一样,我有一个 UIViewController 的子类。当我加载应用程序时,我需要调整一些必须以编程方式放入的元素的大小。当然,大小取决于界面方向:所以,我做到了:

- (void)viewDidLoad {
  switch( [self interfaceOrientation] ) {
     case UIInterfaceOrientationPortrait:
     case UIInterfaceOrientationPortraitUpsideDown:
          NSLog(@"Portrait");
          break:
     case UIInterfaceOrientationLandscapeLeft:
     case UIInterfaceOrientationLandscapeRight:
          NSLog(@"Landscape");
          break;
  }

但无论模拟器/设备的方向如何,在 viewDidLoad/WillAppear 中情况始终是纵向的,即使一切都正确旋转。 在 Plist 中,我添加了所有支持的方向。 提示?

【问题讨论】:

  • 遇到了完全相同的问题!这一直是模拟器中的一个问题。最好的是:插入“willRotate”,它会告诉你它将旋转到横向。接下来它在 layoutSubview 中中断。如果你检查那里的方向,它是纵向的!什么鬼?
  • 你应该重写shouldAutorotateToInterfaceOrientation并在那里返回YES不是很明显吗?如果您这样做了,请将您的代码移至该方法。

标签: uiviewcontroller uiinterfaceorientation


【解决方案1】:

尝试[UIApplication sharedApplication].statusBarOrientation 并检查开关中的UIDeviceOrientationPortraitUIDeviceOrientationPortraitUpsideDown 等,因为根据我的经验,[self interfaceOrientation] 不可靠。

【讨论】:

  • 我从未尝试过使用 statusBarOrientation。该设备的那个是无用的,因为它会告诉您是否将 iPad 放在桌子上。
  • 状态栏提示拯救了我的一天。 UISplitViewController 总是返回 prortrait。
  • statusBarOrientation 返回一个 UIInterfaceOrientation(不是 UIDeviceOrientation),所以你应该使用 UIInterfaceOrientationPortrait(),否则,你偶尔会得到奇怪的结果。
【解决方案2】:

1) 确保 willAppear 中的界面方向返回错误的界面方向。 2)尝试看这里http://developer.apple.com/library/ios/#qa/qa1688/_index.html

3) 确保主视图控制器只是window中的viewcontroller(window中只有一个vc)

【讨论】:

    【解决方案3】:

    我正在处理 iOS 应用程序的旋转和方向。我发现如果在 viewDidAppear 中或之后调用 self.interfaceOrientation 总是会给你一个正确的结果。

    [UIApplication sharedApplication].statusBarOrientation 在直接向 UIWindow 添加视图时应该是首选。

    希望对你有帮助。

    【讨论】: