Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin iOS ObserveOrientationDidChange Landscape or Portrait

I need to detect when the user rotate the device to Landscape orientation. I can detect when the orientation changed with the code below:

UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications();
this.orientationObserver = UIDevice.Notifications.ObserveOrientationDidChange(MyRotationCallback);

private void MyRotationCallback(object sender, NSNotificationEventArgs e)
{
    // Landscape or Portrait ?
}

It works, but I can't tell if it was changed to landscape or portrait. How I can I detect this?

like image 731
Drake Avatar asked May 13 '16 21:05

Drake


1 Answers

Check the property:

UIDevice.CurrentDevice.Orientation

Values:

LandscapeLeft
PortraitUpsideDown
LandscapeRight
Portrait

Example Setup:

In your ViewDidLoad (or similar):

Foundation.NSNotificationCenter.DefaultCenter.AddObserver(new NSString("UIDeviceOrientationDidChangeNotification"), DeviceRotated);

Delegate/Action:

void DeviceRotated(NSNotification notification)
{
    System.Diagnostics.Debug.WriteLine(UIDevice.CurrentDevice.Orientation);
}

Output:

LandscapeLeft
PortraitUpsideDown
LandscapeRight
Portrait
like image 126
SushiHangover Avatar answered Nov 19 '22 04:11

SushiHangover