Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check Device Orientation Change From Portrait To Landscape and Vice-Versa in iPad [duplicate]

I have one split view controller and i am presenting a popover inside it. Now when the device orientation is changing from landscape to portrait i have to run a piece of code & if it is changing from portrait to landscape i have to run another piece of code. How to achieve this in Swift.

like image 264
Swayambhu Avatar asked Apr 13 '16 07:04

Swayambhu


1 Answers

Updated to Swift 4: Add below code in ViewDidLoad:

NotificationCenter.default.addObserver(self, selector: #selector(orientationChanged), name:  Notification.Name("UIDeviceOrientationDidChangeNotification"), object: nil)

Then, create one function like below

@objc func orientationChanged() {

    if(UIDeviceOrientationIsLandscape(UIDevice.current.orientation)){

        print("landscape")
    }

    if(UIDeviceOrientationIsPortrait(UIDevice.current.orientation)){

        print("Portrait")
    }

}

Hope this will helps you :)

like image 103
iVarun Avatar answered Sep 25 '22 17:09

iVarun