How Do I detect the orientation of the device on iOS?

后端 未结 12 913
小鲜肉
小鲜肉 2020-11-30 01:56

I have a question on how to detect the device orientation on iOS. I don\'t need to receive change notifications, just the current orientation itself. This seems to be a rath

12条回答
  •  自闭症患者
    2020-11-30 02:36

    if UIViewController:

    if (UIDeviceOrientationIsLandscape(self.interfaceOrientation))
    {
        // 
    }
    

    if UIView:

    if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
    {
        //
    }
    

    UIDevice.h:

    #define UIDeviceOrientationIsPortrait(orientation)  ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown)
    #define UIDeviceOrientationIsLandscape(orientation) ((orientation) == UIDeviceOrientationLandscapeLeft || (orientation) == UIDeviceOrientationLandscapeRight)
    

    Updated:

    add this code to xxx-Prefix.pch then you can use it anywhere:

    // check device orientation
    #define dDeviceOrientation [[UIDevice currentDevice] orientation]
    #define isPortrait  UIDeviceOrientationIsPortrait(dDeviceOrientation)
    #define isLandscape UIDeviceOrientationIsLandscape(dDeviceOrientation)
    #define isFaceUp    dDeviceOrientation == UIDeviceOrientationFaceUp   ? YES : NO
    #define isFaceDown  dDeviceOrientation == UIDeviceOrientationFaceDown ? YES : NO
    

    usage:

    if (isLandscape) { NSLog(@"Landscape"); }
    

提交回复
热议问题