isInitialViewController

isInitialViewController

我有某些viewControllers,它们由UINavigationController(推和弹出)管理。我想将不同的viewControllers限制为不同的orientations,例如第一个应该仅在Portrait中,第二个在portrait中,第三个在landscape中,第四个可以在portraitlandscape中。我在ViewControllerisInitialViewController上设置了storyBoard

- (BOOL) shouldAutorotate{
return NO;


}

可以正常工作,但是当我将navigation controller(通过推和弹出来管理这四个视图)从isInitialViewController设置为storyBoard时,此函数停止了调用,现在是autoratates。如何使用此autorotating作为UINavigationController停止isInitialViewController这些视图。我使用以下功能取决于它是哪个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;
}

最佳答案

只是子类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

10-04 17:09