Forum Xamarin.iOS
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin iOS UIViewController Portrait Orientation

I am writing to ask how best a specific UIViewController can be set to an orientation of Portrait only - programmatically with C#;

The below code snippets are not preventing an orientation of Landscape in the iOS Simulator - nor on an actual iPod Device;

public override bool ShouldAutorotate() { return true; }

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations() { return UIInterfaceOrientationMask.Landscape; }

Thanks in advance in any insight and/or C# code samples;

Best Answer


  • AxelCharpentierAxelCharpentier FRMember ✭✭

    In your ios project Properties you can specify what orientation your app supports

  • RobertHellestraeRobertHellestrae USMember ✭✭

    @AxelCharpentier said:
    In your ios project Properties you can specify what orientation your app supports

    Thank you for the feedback - yes, info.Plist provides check box options for Device Orientations;

    However, only one UIViewController in my project is required to be in Portrait-only mode;

    UIViewControllers in rest of project can be both orientations - Portrait, as well as Landscape;

  • AxelCharpentierAxelCharpentier FRMember ✭✭

    Sorry I red to quickly :)

  • GregCadmesGregCadmes USUniversity ✭✭
    edited September 2019

    @RobertHellestrae said:
    I am going to select all orientations in the Info.plist file, and programmatically utilize Portrait-only for each View Controller;

    The app project is of the Universal category, and will run on iPhone devices - as well as iPad devices;

    AppDelegate.cs -

    `public bool RestrictRotation = true;

      public UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, IntPtr forWindow)
          if (this.RestrictRotation)
              return UIInterfaceOrientationMask.Portrait;
              return UIInterfaceOrientationMask.AllButUpsideDown;

    ViewDidLoad method of ViewController(s) -

    AppDelegate app = (AppDelegate)UIApplication.SharedApplication.Delegate; app.RestrictRotation = true;

    Excellent, this works perfectly. thank you.

Sign In or Register to comment.