如何在iPhone中以编程方式检查设备的方向?

sus*_*use 26 iphone objective-c ipad ios

可能重复:
iPhone方向

如何在iPhone中以编程方式在任何时间点检查设备的方向?

谢谢.

Azh*_*har 73

这是宏UIDeviceOrientationIsLandscapeUIDeviceOrientationIsPortrait

所以相反单独检查你可以这样做......

   if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
    {
         // code for landscape orientation      
    }
Run Code Online (Sandbox Code Playgroud)

要么

    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
    {
         // code for Portrait orientation       
    }
Run Code Online (Sandbox Code Playgroud)

  • 这在iOS8中不起作用 (2认同)

San*_*dya 15

试试吧.

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

if ( ([[UIDevice currentDevice] orientation] ==  UIDeviceOrientationPortrait)  )
{
   // do something for Portrait mode
}
else if(([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) || ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft))
{
   // do something for Landscape mode
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*gel 9

还尝试:

UIInterfaceOrientationIsPortrait(orientation)
Run Code Online (Sandbox Code Playgroud)

UIInterfaceOrientationIsLandscape(orientation)
Run Code Online (Sandbox Code Playgroud)


Joo*_*ost 7

-[UIViewController interfaceOrientation]

但是,可以独立于当前界面方向检索deviceOrientation:

[[UIDevice currentDevice] orientation]

阅读文档以获取更多信息,您需要做更多的事情才能使其工作.