首先,您需要创建一个分类:
UINavigationController+Rotation_IOS6.h
#import <UIKit/UIKit.h>
@interface UINavigationController (Rotation_IOS6)
@end
UINavigationController+Rotation_IOS6.m:
#import "UINavigationController+Rotation_IOS6.h"
@implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
@end
接下来,在你想要仅横向显示的类中实现这些方法:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
如果您正在使用UITabBarController,则只需将UINavigationController替换为UITabBarController即可。在长时间的搜索之后,这个解决方案对我很有效!我曾经和你现在一样处于同样的情况!
编辑
所以,我看到了你的示例。你需要做出一些改变。
1- 为UINavigationController类创建一个新类别。将该类命名为UINavigationController+Rotation_IOS6(.h和.m)。
2- 您不需要实现
preferredInterfaceOrientationForPresentation
方法。您的类别应该像这样:
#import "UINavigationController+Rotation_IOS6.h"
@implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
@end
3 - 如果您想在仅横向旋转的情况下旋转类,请在实现中包含以下内容,就像这样:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
4 - 我建议您在想要横屏的类中,还包括iOS 5的自动旋转方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}