问题
I am using iOS 6.0 beta and my rotations do not work anymore.
Where can I set a UINavigationControllers supportedOrientations?
According to this http://news.yahoo.com/apple-ios-6-beta-3-changes-182849903.html a UINavigation Controller does not consult their children to determine whether they should autorotate.
I am not using shouldAutorotateToInterfaceOrientation: anymore as it is deprecated. Instead I am using supportedInterfaceOrientations: and shouldAutoRotate: and they are working fine until I place a ViewController into a NavigationController (as a Child). From then on the orientations specified in the ViewController do not work anymore. It seems it is using the orientations set by the navigation controller (UIInterfaceOrientationMaskAllButUpsideDown)
How can I set the InterfaceOrientations for the NavigationController so that my ViewControllers are locked to Portrait-Orientation?
Do I have to subclass UINavigationController and set the InterfaceOrientations there? Isn't it bad practise to subclass UINavigationController still in iOS 6.0?
Thanks for you help heaps!
Cheers!
回答1:
If you want it to consult it's children again you can add a category to UINavigationController
@implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end
回答2:
Subclass UINavigationController
//OnlyPortraitNavigationController.h
@interface OnlyPortraitNavigationController : UINavigationController
//OnlyPortraitNavigationController.m
@implementation OnlyPortraitNavigationController
- (BOOL)shouldAutorotate {
return NO;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait; //for locked only Portrait
}
present new subclass navigationController with your portrait ViewController
SomeViewController *onlyPortraitVC = [[SomeViewController alloc]init];
OnlyPortraitNavigationController *portraitNav = [[OnlyPortraitNavigationController alloc]initWithRootViewController:onlyPortraitViewController];
[self presentViewController:portraitNav animated:YES completion:NULL];
this is work on my app hope it can help you.
来源:https://stackoverflow.com/questions/12169517/where-can-i-set-a-uinavigationcontrollers-supportedorientations