在UINavigationcontroller中自动旋转UIViecontrollers的问题

时间:2016-01-21 12:25:14

标签: ios objective-c uiviewcontroller uinavigationcontroller autorotate

我从this tutorial网站下载了一个关于在navingation控制器中旋转视图控制器的示例。

该示例项目功能

  • 支持FirstviewController的potrait
  • 支持第二视图控制器的所有方向(从第一个VC推送)

我需要的是,

  • FirstviewController应支持所有方向
  • 第二个视图控制器(从第一个VC推出)应该只支持potrait。

我做的是,只是交换了视图控制器中的代码

在第一个视图控制器中

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
- (BOOL)shouldAutorotate  
{
    return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{
    return UIInterfaceOrientationPortrait;
}

在第二个视图控制器和

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
    return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

但是,两个视图控制器都在旋转。

如何解决此问题?有任何建议,请分享。

---- ---- EDIT

如果我在推动之前将模拟器旋转到横向,则第二个VC也处于横向视图中。

如果我在potrait中推到第二个vc,第二个VC处于potrait模式(如果我在那里旋转它就不会旋转。)

我将登录customNavigationcontroller NSLog(@"self.topViewController.class %@",self.topViewController.class);。它只是在推送到第二个vc之后才记录第一个视图控制器它没有记录

1 个答案:

答案 0 :(得分:1)

我建议一种更简单的方法来解决这个问题。

选择项目的目标并将其配置如下:

Select your target and set this

然后转到Appdelegate.m并粘贴:

- (UIViewController*)topViewController {
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
// Handling UITabBarController
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
    UITabBarController* tabBarController = (UITabBarController*)rootViewController;
    return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
}
// Handling UINavigationController
else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
    UINavigationController* navigationController = (UINavigationController*)rootViewController;
    return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
}
// Handling Modal views
else if (rootViewController.presentedViewController) {
    UIViewController* presentedViewController = rootViewController.presentedViewController;
    return [self topViewControllerWithRootViewController:presentedViewController];
}
// Handling UIViewController's added as subviews to some other views.
else 
{
    for (UIView *view in [rootViewController.view subviews])
    {
        id subViewController = [view nextResponder];    // Key property which most of us are unaware of / rarely use.
        if ( subViewController && [subViewController isKindOfClass:[UIViewController class]])
        {
            return [self topViewControllerWithRootViewController:subViewController];
        }
    }
    return rootViewController;
    }
}

现在,这是有趣的部分。

将此方法放在AppDelegate.m上,您可以在此决定每个控制器的方向。

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

id presentedViewController = [self topViewController];
NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;

if (window && [className isEqualToString:@"FirstViewController"]) { //FirstviewController should support all orientations
    return UIInterfaceOrientationMaskAll;
} else {
    return UIInterfaceOrientationMaskPortrait; //Second view controller(pushed from first VC) should support potrait alone.
}
}

然后,您可以删除已添加的所有其他方法,例如shouldAutorotateToInterfaceOrientation,shouldAutorotate,supportedInterfaceOrientations,preferredInterfaceOrientationForPresentation。 所有这些方法现在都是不必要的。

<强> ----- -----编辑

只需使用

 [[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationPortrait] forKey:@"orientation"];

在我需要potrait的所有视图控制器中都可以解决问题。没有其他需要的东西。