britain chare
britain chare

Reputation: 99

Detect the landscape orientation left or right

My app supports portrait and landscape -> Both left and right. I am able to detect if its landscape. But not able to detect left or right. Here is my code

if UIDevice.current.orientation.isLandscape {
// Do some task 
}

When user rotates the device i need to detect whether the user rotated to landscape left or landscape right !

Same way inside my above condition I need to check whether its left or right side. How can I detect that?

Thanks

Upvotes: 1

Views: 6572

Answers (2)

Renetik
Renetik

Reputation: 6373

I am doing it like this for now:

public extension UIScreen { 
    public class var isLandscapeLeft: Bool { UIApplication.shared.statusBarOrientation == .landscapeLeft }
    public class var isLandscapeRight: Bool { UIApplication.shared.statusBarOrientation == .landscapeRight }
}

It's as extension to UIScreen just for convenience so its easy to find.. Also to detect when app actually rotates from left to right when I have just landscape enabled, I had to use AppDelegate's function:

func application(_ application: UIApplication, didChangeStatusBarOrientation oldStatusBarOrientation: UIInterfaceOrientation) { ... }

For whatever reason nothing else get's triggered.

Looks like also, you have to use UIApplication.shared.statusBarOrientation when checking from didChangeStatusBarOrientation instead of UIApplication.shared.delegate?.window??.rootViewController?.interfaceOrientation I used for other situations.

Upvotes: 0

Julian Silvestri
Julian Silvestri

Reputation: 2027

I think you are looking for something like this

    if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {


    } else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {

    } else {
        //not landscape left or right
    }

EDIT --------

based on your comments you are looking for interface orientation instead of device orientation.

override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
    var text=""
    switch UIDevice.current.orientation{
    case .portrait:
        text="Portrait"
    case .portraitUpsideDown:
        text="PortraitUpsideDown"
    case .landscapeLeft:
        text="LandscapeLeft"
    case .landscapeRight:
        text="LandscapeRight"
    default:
        text="Another"
    }
    NSLog("You have moved: \(text)")        
}

the code above detects interface orientation... note how the switch statement still uses UIDeviceOrientation

Below is another method that you may want to use

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.current.orientation.isLandscape {
        print("landscape")
    } else {
        print("portrait")
    }
}

Note again that UIDevice Orientation is still used...

Below is a very different but effective approach.

struct DeviceInfo {
struct Orientation {
    // indicate current device is in the LandScape orientation
    static var isLandscape: Bool {
        get {
            return UIDevice.current.orientation.isValidInterfaceOrientation
                ? UIDevice.current.orientation.isLandscape
                : UIApplication.shared.statusBarOrientation.isLandscape
        }
    }
    // indicate current device is in the Portrait orientation
    static var isPortrait: Bool {
        get {
            return UIDevice.current.orientation.isValidInterfaceOrientation
                ? UIDevice.current.orientation.isPortrait
                : UIApplication.shared.statusBarOrientation.isPortrait
        }
    }
}}

Upvotes: 3

Related Questions