Skip to content

Instantly share code, notes, and snippets.

@pauldbau
Last active December 29, 2015 12:19
Show Gist options
  • Save pauldbau/7669772 to your computer and use it in GitHub Desktop.
Save pauldbau/7669772 to your computer and use it in GitHub Desktop.
Fix to Xamarin.Mobile orientation issues on iPads
internal class MediaPickerPopoverDelegate : UIPopoverControllerDelegate
{
NSObject observer;
UIDeviceOrientation orientation;
internal MediaPickerPopoverDelegate (MediaPickerDelegate pickerDelegate, UIImagePickerController picker)
{
this.pickerDelegate = pickerDelegate;
this.picker = picker;
this.orientation = UIDevice.CurrentDevice.Orientation;
this.observer = NSNotificationCenter.DefaultCenter.AddObserver (UIDevice.OrientationDidChangeNotification, DidRotate);
ApplyOrientation(); // Do this to cater for launch of picker when already in landscape
}
public override bool ShouldDismiss (UIPopoverController popoverController)
{
return true;
}
public override void DidDismiss (UIPopoverController popoverController)
{
this.pickerDelegate.Canceled (this.picker);
NSNotificationCenter.DefaultCenter.RemoveObserver (this.observer);
this.observer.Dispose();
}
void DidRotate (NSNotification notice)
{
UIDevice device = (UIDevice)notice.Object;
if (MediaPickerDelegate.IsSameOrientationKind (this.orientation, device.Orientation))
return;
this.orientation = device.Orientation;
ApplyOrientation();
}
public void ApplyOrientation()
{
// Credit: http://stackoverflow.com/a/19071958/1685090
float scaleFactor = 1.3f;
CGAffineTransform rotated;
switch (this.orientation) {
case UIDeviceOrientation.LandscapeLeft:
rotated = CGAffineTransform.MakeRotation((float)(Math.PI * -90 / 180));
rotated.Scale(scaleFactor, scaleFactor);
picker.CameraViewTransform = rotated;
break;
case UIDeviceOrientation.LandscapeRight:
rotated = CGAffineTransform.MakeRotation((float)(Math.PI * 90 / 180.0));
rotated.Scale(scaleFactor, scaleFactor);
picker.CameraViewTransform = rotated;
break;
case UIDeviceOrientation.PortraitUpsideDown:
rotated = CGAffineTransform.MakeRotation((float)(Math.PI * 180 / 180.0));
picker.CameraViewTransform = rotated;
break;
default:
rotated = CGAffineTransform.MakeRotation(0);
picker.CameraViewTransform = rotated;
break;
}
}
private readonly MediaPickerDelegate pickerDelegate;
private readonly UIImagePickerController picker;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment