自动旋转 iOS 8 导航控制器

Auto Rotation iOS 8 Navigation Controller

我有某些 viewControllersUINavigationController(推送和弹出)管理。我想将不同的 viewControllers 限制为不同的 orientations 就像第一个应该只在 Portrait 中,第二个在 portrait 中,第三个在 landscape 中,第四个可以是portraitlandscape 两者。我在 storyBoardisInitialViewController 上设置了 ViewController

- (BOOL) shouldAutorotate{
return NO;

}

工作没有任何问题,但是当我将 navigation controller(通过推送和弹出管理这四个视图)设置为 storyBoardisInitialViewController 时,此函数停止被调用,现在 autoratates。如何使用此 UINavigationController 作为 isInitialViewController 来停止 autorotating 这些视图。我使用以下函数取决于它是ViewController

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation == UIDeviceOrientationPortrait);//choose portrait or landscape}

- (BOOL) shouldAutorotate{
return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
//return UIInterfaceOrientationMaskLandscape;
return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{ 
//    return UIInterfaceOrientationLandscapeLeft |
//    UIInterfaceOrientationLandscapeRight;
return UIInterfaceOrientationPortrait;
}

如果您为 UINavigation Controller 和 Override 界面方向方法创建 Objective C 类别会怎么样...我想您会尝试控制 Autorotation。

Objective C 类别 http://rypress.com/tutorials/objective-c/categories

只需继承 UINavigationController 并覆盖适当的方法:

.h 文件:

@interface CustomUINavigationController : UINavigationController
@property   BOOL canRotate;
@end

.m 文件:

@implementation CustomUINavigationController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotate
{
    return self.canRotate;
}
@end