Showing posts with label iOS6. Show all posts
Showing posts with label iOS6. Show all posts

Wednesday, October 17, 2012

Change orientation automatically with iOS6 and MonoTouch

Orientation and auto rotation in iOS6 with MonoTouch is slightly less obvious then I had hoped. It took me a little while to get it right, so maybe this will help others. To get your app to auto-rotate you will need to make sure you have done the following:

  1. Edited your Info.plist to specify allowed orientations
  2. Make sure you have RootController assigned to your view. Without this auto rotation will not work and you will get a compile time warning - "Application windows are expected to have a root view controller at the end of application launch". 

Do the following in AppDeligate.cs

UIWindow window;

public override bool FinishedLaunching (UIApplication app, 
                                           NSDictionary options)
{
     window = new UIWindow (UIScreen.MainScreen.Bounds);
     
     // change this to your controller
     var cvc = new SomeViewController(); 
     window.RootViewController = cvc; 
   
     // make the window visible
     window.MakeKeyAndVisible ();

     return true;
}


Once you are done with this, you should get auto-rotation!

Next, you can specify allowed orientations (if you want to limit to a specific set) either in AppDeligate.cs (globally) or in your controller level using the following override:

public override UIInterfaceOrientationMask 
                  GetSupportedInterfaceOrientations 
                     (UIApplication application, UIWindow forWindow)
{
  return UIInterfaceOrientationMask.Landscape;
}
Also, on the controller level, you can disable auto rotation all together using the following:

public override bool ShouldAutorotate()
{
         return false;
}

Hope this helps! Leave me a comment if this worked for you.