Can't prevent screen rotation

jbrightjbright USMember ✭✭

Okay, I feel like an idiot because I've done this before but I've been stuck now for a couple of days.

In my app I do want to allow rotation in many (most) cases. So in my Info.plist file I have 3 of the 4 device orientations selected (which I think is the default when creating a new project). On a couple of screens I want to keep the app in Portrait mode. In a plain vanilla UIViewController class I went ahead and added:

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

public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation()
{
  return UIInterfaceOrientation.Portrait;
}

and for good measure

public override bool ShouldAutorotate()
{
  return false;
}

Yet the damn thing still rotates! What gives? Am I missing something obvious?

JB

Posts

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @jbright,

    Don't worry, you are not alone. I have had the same "couple of days" frustration on this subject before too. Either its unnecessarily difficult to do this, or we are both ...well... doing it wrong. =P

    Are these "couple of screens" in a UINavigationController, UITabController or some other parent controller?

  • jbrightjbright USMember ✭✭

    They're in a UINavigationController that I use for first time users. They can register or login so it's like the first 2 screens that they see. Do I have set something at the UINavigationController level?

  • jbrightjbright USMember ✭✭

    You've got to be kidding me. This works.

        public class TestNavController : UINavigationController
        {
            public TestNavController(UIViewController rootViewController) : base(rootViewController) {}
            public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
            {
                return UIInterfaceOrientationMask.Portrait;
            }
        }
    
  • jbrightjbright USMember ✭✭

    (Basically a hack to use an overloaded UINavigationController to achieve that result. Is there a cleaner way to do this?

    JB

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    I was going to mention a hack like that. That is how I solved some of my issues too. Let me know if you find a better solution.

  • jbrightjbright USMember ✭✭

    Yeah, right now nothing more elegant jumps to mind. Thanks for mentioning the UINavigationController though as that was an ah-ha moment. It seemed more intuitive when it was the actual controller that was being presented.

    JB

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    Per the iOS SDK docs, in iOS 6+, only the root controller (or view controller presented to fill the entire screen) receives the supportedInterfaceOrientations call, so this isn't really a hack. It appears to be what Apple intended.

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    I was thinking it was a hack because in the past subclassing UINavigationController was not advised. However, I just checked the docs and found this:

    The UINavigationController class implements a specialized view controller that manages the navigation of hierarchical content. This navigation interface makes it possible to present your data efficiently and makes it easier for the user to navigate that content. You generally use this class as-is but in iOS 6 and later you may subclass to customize the class behavior.

  • HiltonMenezesHiltonMenezes BRMember ✭✭
    edited January 2014

    I am sorry, i am trying to prevent my screen from rotating and im not being able to do that where should i put that code or how to use it with the navigation controler of my uiViewController? Do i need to change something in the AppDelegate or something?
    Thanks

Sign In or Register to comment.