【问题标题】:How to detect Device Orientation before viewDidLoad?如何在 viewDidLoad 之前检测设备方向?
【发布时间】:2013-11-11 11:53:21
【问题描述】:

我需要在控制器中运行 viewDidLoad 方法之前检测设备方向。我尝试了针对其他问题的所有解决方案,但我无法解决。

在 xCode 5 和 iOS 7 中使用 SotryBoard 的最佳方式是什么?

提前感谢您!

编辑:实际上我正在使用

- (void)viewDidLoad {

    [super viewDidLoad];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

        switch ([[UIDevice currentDevice] orientation]) {
            case (UIDeviceOrientationPortrait):
                // landscape layout
                break;

            default:
                // portrait layout
                break;
        }

但它不起作用......

已解决:使用

- (void)viewDidLoad {

    [super viewDidLoad];

    if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        // Portrait
    }

    if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight){
        // Landscape
    }
}

【问题讨论】:

  • 不要混淆 UIDeviceOrientation 和 UIInterfaceOrientation
  • 您不应该在viewDidLoad 中进行布局。在这里您可以设置数据模型和初始化视图,但您的所有布局都应该在viewWillLayoutSubviews 中完成,其中self.view.frame 将准确地表示当前帧,并考虑旋转。
  • 它是否也适用于storyBoard?

标签: ios orientation device uiinterfaceorientation


【解决方案1】:

[[UIDevice currentDevice] orientation]是你需要的吗?

【讨论】:

  • 如果不打电话给[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications],这可能行不通
【解决方案2】:

尝试检查状态栏方向:

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

在 viewDidLoad 之前还有一些其他的视图控制器方法被触发。看看 awakeFromNib 和 initWithCoder。

【讨论】:

  • 如果状态栏被隐藏,这不起作用任何其他建议,请分享,谢谢回答...
猜你喜欢
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
  • 2013-10-29
  • 2015-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多