IOS 6 Orientation

2019-09-09 05:40发布

问题:

I am working on a project and facing a problem. My problem is that i am making a project for 5.0 and above, My project is all in portrait view but only one view has both view (Landscape and portrait) i am using NavigationController custom class and check Orientations like this in custom navigation class `

    - (NSUInteger)supportedInterfaceOrientations
  {
int interfaceOrientation = 0;

if (self.viewControllers.count > 0)
{
    id viewController;
    for (viewController in self.viewControllers)
    {
       if ([viewController isKindOfClass:([CalenderViewController class])])
        {
            interfaceOrientation = UIInterfaceOrientationMaskAll;
        }
        else
        {
            interfaceOrientation = UIInterfaceOrientationMaskPortrait;
        }
    }
}
return interfaceOrientation;
}` 

CalenderViewController is my view that supported both view this code works fine for popview when i pop a view to CalenderViewController, this works fine but when i push a new view controller on CalenderViewController that has only portrait view then the new viewcontroller remains in landscape, whereas it should be in portrait mode .Looking forward for a solution thanks

回答1:

In cases like this, you shouldn't return NO on shouldAutorotate. If you do that, supported orientations won't even be checked and your controller will be stuck on the last orientation used.

Instead, just return YES on shouldAutorotate and specifiy a single orientation (portrait) in supportedInterfaceOrientations, like you already do.

Returning YES on shouldAutorotate isn't meant only for when you want you controller to freely rotate to other orientations. It should also be used when you might need your controller to rotate back to its only supported orientation after popping or pushing a view that does differently.