【问题标题】:Transitioning to landscape rotation within a uinavigationcontroller在 uinavigationcontroller 中转换为横向旋转
【发布时间】:2011-02-24 18:14:55
【问题描述】:

我想从 uinaviagtioncontroller 中推送一个横向视图,它的初始视图是纵向的... 因此,您单击一个按钮或 tableviewcell,屏幕将旋转 90° 进入新视图..

我将如何管理它?这类似于 Youtube 应用在从电影信息屏幕转换到实际电影时的工作方式。

(注意:我对用户拿着设备的旋转方式不感兴趣)

【问题讨论】:

    标签: iphone objective-c cocoa-touch uinavigationcontroller


    【解决方案1】:

    实现以下方法,当视图被推送到导航堆栈时应该调用该方法:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }
    

    这应该会在您将所有内容推送到堆栈后立即自动向侧面旋转,因为视图不支持纵向。

    如果我错了,但没有,您始终可以通过设置状态栏方向来强制水平方向:

    - (void) viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:animated];
    }
    

    【讨论】:

      【解决方案2】:

      这是我的解决方案,基于此页面上的 Ed Marty 和此处的 Simon - http://simonwoodside.com/weblog/2009/2/27/iphone_programming_how_to_switch/

      在前面的视图中,从您的按钮或表格单元格选择方法中调用它

      - (void) launchLandscape
      {
          LandscapeViewController *controller = [[LandscapeViewController alloc] initWithNibName:@"LandscapeViewControllerView" bundle:nil];
          [self.navigationController setNavigationBarHidden:YES];
          controller.hidesBottomBarWhenPushed = YES;
          [[self navigationController] pushViewController:controller animated:NO];
          [controller release];
      
      }
      

      然后在横向视图中

      - (void)viewWillAppear:(BOOL)animated { 
          [[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
          [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
          CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation( degreesToRadian(90) );
          landscapeTransform = CGAffineTransformTranslate( landscapeTransform, +90.0, +90.0 );
          [self.view setTransform:landscapeTransform];
          [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
      }
      
      
      -(IBAction)backButton:(id)sender
      {
          [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
          [[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
          [self.navigationController setNavigationBarHidden:NO];
          [self.navigationController popViewControllerAnimated:NO];
      }
      

      这有点“突然”。您不能真正在其中任何一个上放置动画,因为它看起来太有问题了。我想可以通过推送带有动画的中间视图(纯黑色视图)然后自动执行上述操作以进入最终景观来改进它。

      【讨论】:

        【解决方案3】:

        从电影信息屏幕转换到实际电影时,YouTube 应用中出现的不是导航界面,而是模态视图。这总是可靠的:如果您以模态方式显示视图(使用presentModalViewController)并且它只能出现在一个方向上,则应用会旋转到该方向。

        因此,解决方案是,不要将横向视图推送到导航控制器中;将其呈现为模态视图。

        好的,但也许您想欺骗用户相信我们仍在导航界面中?然后给模态视图一个导航界面,配置成看起来像主导航界面!因此,当您呈现模态视图时,它会向用户显示,就好像导航界面已经旋转(尽管不会有旋转动画)。

        现在,唯一的问题是,如果我们查看它的根视图,模态视图中的导航界面没有返回按钮。这打破了这种错觉(并使用户很难回来)。解决方案是一个技巧:将横向视图 两次 推送到导航界面,然后再将其呈现为模态视图。这样,导航界面中显示的是堆栈上的 second 视图,因此有一个返回按钮。然后,在导航控制器委托中,当用户尝试返回到您所知道的根级别时,抓住后退按钮并关闭模式视图。所以:

        - (void) doButton: (id) sender { // appear to navigate into a landscape view
            SecondViewController* sec = [[SecondViewController alloc] init];
            sec.title = self.title; // to give the correct Back button title
            UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:sec];
            SecondViewController* sec2 = [[SecondViewController alloc] init];
            [nav pushViewController:sec2 animated:NO];
            [self presentModalViewController:nav animated:YES];
            nav.delegate = self; // so that we know when the user navigates back
        }
        
        // and here's the delegate method
        - (void)navigationController:(UINavigationController *)navigationController 
              willShowViewController:(UIViewController *)viewController 
                            animated:(BOOL)animated {
            if (viewController == [navigationController.viewControllers objectAtIndex:0])
                [self dismissModalViewControllerAnimated:YES];
        }
        

        【讨论】:

        • 您好,感谢您的解决方案。当我将屏幕从横向旋转到纵向(反之亦然)多次)然后它会发出内存警告,并且在 3-4 多次旋转后,应用程序正在崩溃。您能否建议卸载 SecondViewController 的正确方法,以便这两个实例在被解雇后不再占用内存。谢谢
        • @WQS 你应该问这个作为你自己的不同问题!如果内存没有被释放,你可能遇到了其他问题
        猜你喜欢
        • 1970-01-01
        • 2011-10-03
        • 2010-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-17
        • 1970-01-01
        相关资源
        最近更新 更多