是否可以为UINavigationController中的viewControllers提供不同的方向?

时间:2012-06-07 14:10:36

标签: iphone objective-c ios screen-orientation uiinterfaceorientation

我希望所有视图控制器仅支持纵向模式,但一个视图控制器允许将其称为“LandscapeSupportViewController”,它也应支持横向模式。

问题是当我在横向模式下使用LandscapeSupportViewController然后推送仅支持纵向模式的新视图控制器时,推送的视图控制器也将处于横向模式!我怎么能强迫它成为肖像?

我看到很少有应用程序执行此操作,例如Skype iPhone应用程序,“消息”选项卡仅为纵向 - >然后,如果您按下以输入消息本身,您将获得支持格局的视图控制器,因为在用户聊天时启用横向模式是有意义的 - >然后,如果您按下查看人物配置文件,将按下纵向推送新的视图控制器!如果你回去也会发生同样的情况,即使你来自风景,也会被迫回到肖像......

由于

4 个答案:

答案 0 :(得分:1)

我让学生尝试完成你想要完成的任务,经过大量研究,普遍的共识是:这是一个坏主意,需要很多(App Store合法)黑客来完成,仍然不会太漂亮(状态栏,例如,拧紧)。您将在Skype应用程序中注意到,当您进入IM部分时,旋转到横向,然后回击,UI“快照”,或者立即重新加载。

这不是一个良好的用户体验,我建议您重新考虑您的设计,使其更符合Apple的建议。

答案 1 :(得分:1)

如果我找到了你,你想在某些条件下改变设备方向。

[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationPortrait animated:NO];

使用上面的行设置你自己的方向,只需将这些行放在if条件中。条件取决于你。

谢谢!

答案 2 :(得分:1)

在推送仅支持肖像的viewController之前写下此行来自landscapeViewController

[appdel.navigationController.view removeFromSuperview];// This navcontroller used with rootviewcontroller
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
[ [UIApplication sharedApplication].self.delegate.window addSubview:appdel.navigationController.view];
self.navigationController.navigationBar.hidden=NO;

答案 3 :(得分:1)

这是一个解决方案。 您可以为管理视图控制器方向的UINavigationController添加类别。请参阅以下代码:

@interface UINavigationController (MyViewOrientations)
@end

@implemetation UINavigationController (MyViewOrientations)

- (BOOL)supportLandscapeModeForViewController:(UIViewController *)controller {
    return [controller isKindOfClass:[LandscapeSupportViewController class]]
}

- (NSUInteger)supportedInterfaceOrientation {
    UIViewController *controller = [self visibleViewController];
    NSUInteger orientationMasks = UIInterfaceOrientationMaskPortrait
    if([self supportLandscapeModeForViewController:controller]) {
        orientationMasks |= UIInterfaceOrientationMaskLandscapeLeft;
        orientationMasks |= UIInterfaceOrientationMaskLandscapeRight;
    }
    return orientationMasks;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    UIViewController *controller = [self visibleViewController];
    if([self supportLandscapeModeForViewController:controller]) {
        return UIInterfaceOrientationLandscapeLeft; // Your call
    }
    else {
        return UIInterfaceOrientationPortrait;
    }
}

- (BOOL)shouldAutorotate {
    UIViewController *controller = [self visibleViewController];
    return [self supportLandscapeModeForViewController:controller];
}
@end

如果情况更复杂,不同的观点支持不同的方向。您可以在视图控制器中覆盖“supportedInterfaceOrientation”,“preferredInterfaceOrientationForPresentation”,“shouldAutorotate”,并使用“visibleViewController”委托来自UINavigationController类别代码的调用。