1

My goal is to force landscape orientation on a single view controller, not the entire app (to be exact: I want to make my camera view controller to be landscape only)

I've been running into the issue not being able to force landscape on iOS 10 devices with Xamarin.iOS.

I've got it working on iOS 9 or lower by overriding the following methods in the view controller that is supposed to be in landscape only. However, these methods don't seem to be called on iOS 10.

public override bool ShouldAutorotate()
{
    return true;
}
public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation()
{
    return UIInterfaceOrientation.LandscapeLeft;
}
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
{
    return UIInterfaceOrientationMask.Landscape;
}

I also tested calling this method from ViewDidLoad (I'm not using this line anymore and can't tell if it has any effect)

//AppDelegate
public void ChangeOri()
    {
        UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));
    }

Any suggestions on a possible workaround?

Csharpest
  • 1,258
  • 14
  • 32
  • what you want to do ? – KKRocks Apr 17 '17 at 11:08
  • I want to force landscape orientation on a single view controller. I updated my question to be more exact, ty! – Csharpest Apr 17 '17 at 11:18
  • can you understand objective c code ? – KKRocks Apr 17 '17 at 11:31
  • Not too well but i can give it a try. Despite that, the code has to be translatable and working in xamarin – Csharpest Apr 17 '17 at 11:35
  • try this if any query ask me : http://stackoverflow.com/a/24928474/3901620 – KKRocks Apr 17 '17 at 11:36
  • using this code you need to use for enable and disable landscape mode : AppDelegate *appDelegate = [UIApplication sharedApplication].delegate; [appDelegate setShouldRotate:YES]; // or NO to disable rotation – KKRocks Apr 17 '17 at 11:38
  • This seem to look like my "bool ShouldAutorotate()", it does work on any iOS except for iOS 10. I think this won't help me – Csharpest Apr 17 '17 at 11:50
  • try it is working fine in ios 10 . you need to add this two line in viewWillappear . – KKRocks Apr 17 '17 at 11:51
  • both are different approach . – KKRocks Apr 17 '17 at 11:51
  • I will test it tomorrow, I can't test it today. But I think "ShouldAutorotate" is the Xamarin (C#) version of "setShouldRotate". Does that mean I have to call "setShouldRotate" / "Shouldautorotate" in my AppDelegate? not on ViewController? – Csharpest Apr 17 '17 at 11:59
  • in viewcontroller's viewwillappera method . – KKRocks Apr 17 '17 at 12:02
  • Oh yeah you said that, sorry. I will test it tomorrow! thanks! – Csharpest Apr 17 '17 at 12:02
  • Sadly, this did not work, still looking for an answer! The solution u told me works for devices with >iOS 10 – Csharpest Apr 18 '17 at 10:49
  • what happens and did you implement properly ? – KKRocks Apr 18 '17 at 10:50
  • C# Xamarin: I called `ShouldAutorotate();` in `ViewWillAppear(bool animated)` , ShouldAutorotate is overriden in the viewcontroller to `return true` . I did this in `ViewDidAppear` as well, didnt fix the issue. Orientation is not changing at all – Csharpest Apr 18 '17 at 10:56
  • did you declared ShouldAutorotate in appdelegate ? – KKRocks Apr 18 '17 at 11:00
  • Oh now I understand that u have declared it in AppDelegate. Ok im sorry. Well I have NOT declared it in AppDelegate, honestly I dont understand what the function has to look like in C# version – Csharpest Apr 18 '17 at 11:05
  • check that link i given ....and follow that steps . – KKRocks Apr 18 '17 at 11:06
  • Look at my edited post, do you think this is the correct implementation in AppDelegate? I don't see how this would help – Csharpest Apr 18 '17 at 11:07
  • yes correct ...now true that (appdelegate.shouldAutoRotate) variable in ViewWillAppear while you need landscape mode. **(Note : Do false for portrait)** – KKRocks Apr 18 '17 at 11:10
  • or another solution : NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft]; [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; – KKRocks Apr 18 '17 at 11:14
  • The first workaround did not work. Workaround 2: `UIDevice.Orientation` is readonly. I have to change it with `UIDevice.SetValueForKey` ? I will test this now! ty – Csharpest Apr 18 '17 at 11:19
  • Still nothing works for iOS 10... – Csharpest Apr 18 '17 at 11:49

1 Answers1

3

I finally found an answer, it has nothing to do with iOS 10 itself.

I had the issue on iPad devices. iPads with iOS9 or higher have received a new function: "Multitasking". Refer to this thread to learn more about.

The crux is, when your app is set to be multitasking you can no longer change orientations from code. You have to enable fullscreen mode for the whole app to make the lines of code be called that I posted in my question above.

Set in your plist: UIRequiresFullScreen to true to do so.

Csharpest
  • 1,258
  • 14
  • 32