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

Answers

  • 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 5

    @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;

      [Export("application:supportedInterfaceOrientationsForWindow:")]
      public UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, IntPtr forWindow)
      {
          if (this.RestrictRotation)
              return UIInterfaceOrientationMask.Portrait;
          else
              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.