强制控制器在“纵向”或“横向”中更改其方向

时间:2013-09-23 11:10:34

标签: objective-c ipad cocoa-touch

我有以下控制器,(我在iPad中选择了所有类型的方向模式)

这是我的iPad故事板布局

Custom NavigationController > Loading Ctrl > Main Controller.

我的自定义导航包含

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

在我的加载控制器

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM())
        return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
    else
        return UIInterfaceOrientationMaskPortrait;
}

supportedInterfaceOrientations像往常一样被调用,一切似乎都没问题,但当我使用performSegue推送我的主控制器时

-(NSUInteger)supportedInterfaceOrientations
{
    if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM())
        return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
    else
        return UIInterfaceOrientationMaskPortrait;
} 

MainController中不再有调用。那是为什么?

2 个答案:

答案 0 :(得分:3)

有一招。

从应用程序中获取状态栏并旋转它。

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

以模态方式创建和显示和关闭空视图控制器。

UIViewController *mVC = [[UIViewController alloc] init];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];

现在你的设备已被迫旋转。您现在可以使用导航控制器切换到正确的视图控制器或推送控制器。

答案 1 :(得分:1)

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation == UIInterfaceOrientationPortrait);//choose portrait or landscape
}
- (BOOL) shouldAutorotate{
return NO;
}
- (NSUInteger) supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;//choose portrait or landscape, same as above
}