添加新的 Objective-C 类(UINavigationController 的子类)并将以下代码添加到 .m 文件中
-(NSUInteger)supportedInterfaceOrientations
{
NSLog(@"supportedInterfaceOrientations = %d ", [self.topViewController supportedInterfaceOrientations]);
return [self.topViewController supportedInterfaceOrientations];
}
-(BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// You do not need this method if you are not supporting earlier iOS Versions
return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
添加新类后,转到您的 ViewController 类并进行以下更改
- (BOOL)shouldAutorotate // iOS 6 autorotation fix
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations // iOS 6 autorotation fix
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation // iOS 6 autorotation fix
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
在 shouldAutorotate 中, shouldAutorotateToInterfaceOrientation: 如果您希望 ViewController 支持多方向则返回 YES ,否则返回 NO ,同样在 houldAutorotateToInterfaceOrientation: 方法中为特定 ViewController 传递您想要的 Orintation ,对所有视图控制器重复相同的操作。
这样做的原因:-
1:虽然您可以将任何viewController 的preferredInterfaceOrientationForPresentation: 更改为特定方向,但是由于您使用的是UINavigationController,您还需要为您的UINavigationController 覆盖supportedInterfaceOrientations
2:为了覆盖 UINavigationController 的 supportedInterfaceOrientations,我们将 UINavigationController 子类化并修改了与 UINavigation Orientation 相关的方法。
希望它会帮助你!