获取当前界面方向的不同方法?

Anh*_* Do 63 orientation ios

我知道有三种方法可以获得当前的界面方向:

  • self.interfaceOrientation
  • [[UIApplication sharedApplication] statusBarOrientation]
  • [[UIDevice currentDevice] orientation]

他们之间有什么不同?

ber*_*ium 112

self.interfaceOrientation返回UIInterfaceOrientation,接口的当前方向.它是UIViewController中的一个属性,只能在UIViewController类中访问它.

[[UIApplication sharedApplication] statusBarOrientation]返回UIInterfaceOrientation,即应用程序状态栏的当前方向.您可以在应用程序的任何位置访问该属性.我的经验表明,这是检索真实界面方向的更有效方法.

[[UIDevice currentDevice] orientation]返回UIDeviceOrientation,设备方向.您可以在应用程序的任何位置访问该属性.但请注意,UIDeviceOrientation并不总是UIInterfaceOrientation.例如,当您的设备位于普通表上时,您可以获得意外的价值.

  • 在iOS 8中不推荐使用`self.interfaceOrientation`. (6认同)
  • @Pwner UI的方向不一定是设备的方向.例如,如果您的应用仅支持横向,并且您将设备转为纵向,则设备方向将显示为纵向,但UI的方向仍为横向(并且它也将保持横向).此外,如果设备在桌子上是平的,它不能确定它的方向是什么,但它确实知道UI在任何时候的方向. (2认同)

小智 25

[[UIApplication sharedApplication] statusBarOrientation] - 它总是等于四个值中的一个:肖像,左侧风景,右侧风景和上下颠倒.

[[UIDevice currentDevice] orientation] - 这一个等于标准四个值,并且:未知,面朝上或面朝下.

[[UIApplication sharedApplication] statusBarOrientation] - 更优先获得界面定位的方式.

  • 12个偏头痛和5个溃疡之后,我可以确认`[[UIApplication sharedApplication] statusBarOrientation]`是一种更可靠,更简单的检测和管理方向变化的方法. (8认同)