代码之家  ›  专栏  ›  技术社区  ›  Massimo Cafaro

横向模式下的选项卡控制器和导航控制器,第二集

  •  6
  • Massimo Cafaro  · 技术社区  · 15 年前

    我有一个uitabarcontroller,每个选项卡处理不同的uiviewcontroller,根据需要推送堆栈新的控制器。在我需要的两个选项卡中,当达到特定的控制器时,可以旋转iPhone并以横向模式显示视图。经过大量的努力,我发现必须对uitabarcontroller进行子类化,以覆盖shouldAutoToInterfaceOrientation。但是,如果我在实现中简单地返回yes,则会产生以下不良的副作用:

    旋转iPhone时,每个选项卡中的每个控制器都会自动置于横向模式。

    即使在每个控制器中覆盖shouldAutoToInterfaceOrientation以返回no也不起作用:当iPhone旋转时,控制器将处于横向模式。

    我在子类uitabarcontroller中实现了shouldAutoToInterfaceOrientation,如下所示:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        if([self selectedIndex] == 0 || [self selectedIndex] == 3)
            return YES;
    
        return NO;
    }
    

    因此,只有我感兴趣的两个选项卡才能真正获得对景观模式的支持。 在特定选项卡的堆栈上是否有支持特定控制器的横向模式的方法?

    我尝试过,但没有成功

    (bool)shouldAuto接口方向:(ui接口方向)接口方向{

    if([self selectedIndex] == 0 || [self selectedIndex] == 3)
    {   
       if ([[self selectedViewController] isKindOfClass: [landscapeModeViewController class]])
               return YES;
        }
    
         return NO;
    

    }

    另外,我尝试使用委托方法didselectViewController,但没有成功。 非常感谢您的帮助。 谢谢您。

    7 回复  |  直到 11 年前
        1
  •  4
  •   Rich Aston    15 年前

    这对我很有用:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        if(self.selectedIndex == 0 && [[[self.viewControllers objectAtIndex:0] visibleViewController] isKindOfClass:[MyViewController class]])
            return YES;
        else
            return NO;
    }
    
        2
  •  7
  •   Zargony    15 年前

    这是UitabarController的一个扩展,委托调用它 shouldAutorotateToInterfaceOrientation 到当前选定的子控制器。使用这个扩展,您不再需要子类UItabarController,并且可以使用 应注意面间方向 在你的控制器里,就像你所期望的那样。

    uitabarcontroller+自动旋转.h:

    #import <UIKit/UIKit.h>
    
    @interface UITabBarController (Autorotate)
    @end
    

    uitabarcontroller+自动旋转.m:

    #import "UITabBarController+Autorotate.h"
    
    @implementation UITabBarController (Autorotate)
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        UIViewController *controller = self.selectedViewController;
        if ([controller isKindOfClass:[UINavigationController class]])
            controller = [(UINavigationController *)controller visibleViewController];
        return [controller shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    }
    
    @end
    
        3
  •  3
  •   Joe D'Andrea    15 年前

    我已经用了一段时间了(从我的应用程序的标签栏控制器),没有问题:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
         return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    }
    

    这样,在适当的风险投资中,我们可以 真实的 在这种情况下,请检查照片库视图(还有什么?):

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    

    对于给定的导航控制器,我的图库视图甚至不在堆栈的顶部。它仍然会被调用。

    唉,我才发现 当风险投资公司潜伏在 更多视图控制器 (与四个主要选项卡相反)。在这种情况下,我的画廊风投从来没有接到过电话。我想这是因为我一直打电话给的VC实际上是所选选项卡上的导航控制器,它 然后 将内容传播到适当的VC,在本例中是我的照片库VC。但对更多的风投来说,事情并不那么顺利…aa然后事情就从那里旋转地向下发展。:

    我尝试使用Andreas所做的修改(参见本文其他部分),但没有任何效果。欢迎线索!

        4
  •  2
  •   user247212    15 年前

    我遇到的问题和你在与UitabarController合作时遇到的问题一样。我需要控制哪些uiviewcontrollers允许旋转,哪些不允许旋转。我的主要问题是更多的标签。我不想让更多选项卡中包含的任何uiviewControllers旋转。

    我的解决方案是创建自己的uitabbarController,我称之为myTabbarController:

    @interface MyTabBarController : UITabBarController <UITabBarDelegate> {
    
    }
    

    然后我实现了shouldAutoToInterfaceOrientation方法:

    @implementation MyTabBarController
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
     UIViewController *controller = [self selectedViewController];
    
     if ((controller == [self moreNavigationController]) || ([self selectedIndex] == 4))
     {
      return interfaceOrientation == UIInterfaceOrientationPortrait;
     }
    
     return [controller shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    }
    
    @end
    

    我需要发现是否选择了“更多”选项卡。这是一个两步的过程;当最初选择了更多选项卡时,API返回的SelectedIndex高于4,因此我需要将所选控制器与MoreNavigationController进行比较。

    如果从“更多”选项卡中选择了uiviewController,则selectedIndex最终为4,但selectedController不再是moreNavigationController,而是选定的uiviewController。

    这个 if((controller==[self moreNavigationController])([self selectedIndex]==4))。 解决这个问题。

    现在,当我运行应用程序时,“更多”选项卡中的uiviewcontrollers不会旋转。我希望这能帮助其他和我一样遇到同样问题的开发人员。

    埃米利奥

        5
  •  1
  •   AOphagen    11 年前

    根据我在这里和其他地方所看到的,我已经将使用该方法的解决方案缝合在一起。 shouldAutorotate 自古以来 shouldAutorotateToInterfaceOrientation 已弃用。

    我把它放在了一个类别中,以UitabarController。 我希望这是可以接受的!

    // call to method shouldAutorotate replaces call to method shouldAutorotateToInterfaceOrientation (deprecated)
    -(BOOL)shouldAutorotate
    { // check whether selected view controller should autorotate    
      UIViewController *controller = self.selectedViewController;
      if ([controller isKindOfClass:[UINavigationController class]])
        { // in case it is a navigation controller: get visible view of that
          controller = [(UINavigationController *)controller visibleViewController];
        }
      return [controller shouldAutorotate];
    }
    
        6
  •  0
  •   jangelo42    14 年前

    谢谢,谢谢,谢谢。这两天来一直在思考如何做到这一点。当你有一个带导航控制器的TabbarController时,我接受你所有的帮助。

    -(bool)shouldAuto接口方向:(ui接口方向)接口方向{

    UIViewController *controller = self.selectedViewController;
    if ([controller isKindOfClass:[UINavigationController class]])
        controller = [(UINavigationController *)controller visibleViewController];
    
    if([controller isKindOfClass:[LOCviewcontroller class]])
        return YES;
    else
        if([controller isKindOfClass:[personWebSiteView class]])
            return YES;
    else return NO; 
    

    }

    任何对新奥派编码员代码的批评都是值得赞赏的…杰克

        7
  •  0
  •   Manne W    14 年前

    是否确实可以将UItabarController子类(如上面接受的答案中所建议的那样)划分为子类?

    我明白苹果说的“你永远不应该把uitabarcontroller或uinavigationcontroller划分为子类”——或者我误解了吗?

    无论如何,我找到了 this tutorial 其中,它们子类化uiviewcontroller,并在其中放置uitabarcontroller。