Determine Device Orientation in Swift

In this tutorial, you will learn how to determine device orientation in Swift.

By the end of this tutorial, you will have a working Swift code example that you can use in your mobile application.

If you are interested in learning how to determine the width and the height of the screen, please read this tutorial: Determine main screen bounds, height, and width.

Step 1: Determine Device Orientation When the View is About to Appear

When a view is about to appear, you can determine the device’s orientation by checking the UIDevice.current.orientation property. This property returns the physical orientation of the device. In the viewWillAppear(_:) method of your view controller, you can call a custom function to determine the device’s orientation.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    determineMyDeviceOrientation()
}

func determineMyDeviceOrientation() {
    if UIDevice.current.orientation.isLandscape {
        print("Device is in landscape mode")
    } else {
        print("Device is in portrait mode")
    }
}

Step 2: Determine Device Orientation When the Device is Being Rotated to Landscape or Portrait

To handle orientation changes, you can override the viewWillTransition(to:with:) method in your view controller. This method is called whenever the interface orientation changes. Inside this method, you can again call the function to determine the device’s orientation.

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    determineMyDeviceOrientation()
}

Complete Code Example

Here’s the complete code example based on the steps above:

import UIKit

class ViewController: UIViewController {
    var textView: UITextView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        determineMyDeviceOrientation()
    }
    
    func determineMyDeviceOrientation() {
        if UIDevice.current.orientation.isLandscape {
            print("Device is in landscape mode")
        } else {
            print("Device is in portrait mode")
        }
    }
    
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        determineMyDeviceOrientation()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

DeviceOrriantationSwift

Conclusion

I hope this tutorial was helpful to you.

To learn more about Swift and to find other code examples, check the following page: Swift Code Examples.

Keep coding!

Leave a Reply

Your email address will not be published. Required fields are marked *