首先,您需要创建一个类别:
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 - 在您只想横向旋转的类中,将其包含在实现中,就像这样:
// Rotation methods for iOS 6
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
4 - 我建议在你想要的横向类中也包括 iOS 5 的自动旋转方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}