简体   繁体   English

在加载时检测方向

[英]Detecting orientation on loading

When my simulator is in portrait and when my viewcontroller loads initially , it prints out Landscape instead of Portrait but when I change the orientaiton, it correctly displays the orientation so forth. 当我的模拟器为portrait时,并且当我的视图控制器viewcontroller loads initially ,它会打印出“ Landscape而不是“ Portrait但是当我更改方向时,它会正确显示方向。 I did the following 我做了以下

override func viewDidLayoutSubviews() {
  super.viewDidLayoutSubviews()

  if (UIDevice.current.orientation == UIDeviceOrientation.portrait || UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown)
  {
    print("Portrait")
  }
  else
  {
    print("Landscape")
  }
}

I have no idea why it is displaying wrong orientation when it loads initially but once I rotate everything seems to work. 我不知道为什么它在初始加载时显示错误的方向,但是一旦旋转,一切似乎都可以正常工作。

PS It seems like when simulator initially loads, the orientaiton is unknown , so it is choosing the else condition , how to avoid this from happening and identify the correct orientaiton? PS似乎在模拟器最初加载时,方向是unknown ,所以它在选择else condition ,如何避免这种情况发生并识别正确的方向?

if UIDevice.currentDevice().orientation.isLandscape.boolValue {
    print("landscape")
} else {
    print("portrait")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM