Last active
December 29, 2015 12:19
-
-
Save pauldbau/7669772 to your computer and use it in GitHub Desktop.
Fix to Xamarin.Mobile orientation issues on iPads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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