Iphone tabBarController和NavigationController处于横向模式,第二集

Iphone tabBarController和NavigationController处于横向模式,第二集,iphone,uiviewcontroller,uitabbarcontroller,landscape,Iphone,Uiviewcontroller,Uitabbarcontroller,Landscape,我有一个UITabBarController,每个选项卡处理一个不同的UIViewController,根据需要在堆栈上推送新的控制器。在其中两个选项卡中,当到达特定控制器时,我需要能够旋转iPhone并以横向模式显示视图。经过多次努力,我发现必须将UITabBarController子类化以覆盖shouldAutorotateToInterfaceOrientation。但是,如果我在实现中简单地返回YES,则会产生以下不良副作用: 旋转iPhone时,每个选项卡中的每个控制器都会自动置于横向

我有一个UITabBarController,每个选项卡处理一个不同的UIViewController,根据需要在堆栈上推送新的控制器。在其中两个选项卡中,当到达特定控制器时,我需要能够旋转iPhone并以横向模式显示视图。经过多次努力,我发现必须将UITabBarController子类化以覆盖shouldAutorotateToInterfaceOrientation。但是,如果我在实现中简单地返回YES,则会产生以下不良副作用:

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

即使在每个控制器中重写shouldAutorotateToInterfaceOrientation以返回NO也不起作用:当iPhone旋转时,控制器将处于横向模式

我在子类UITabBarController中实现了shouldAutorotateToInterfaceOrientation,如下所示:

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

    return NO;
}
@interface MyTabBarController : UITabBarController <UITabBarDelegate> {

}
因此,只有我感兴趣的两个选项卡实际上获得了对横向模式的支持。 是否有方法支持特定选项卡堆栈上特定控制器的横向模式

我试过,但没有成功

(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation{

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

     return NO;
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; 
}

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

这对我很有用:

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

这里是UITabBarController的扩展,它将对
shouldAutorotateToInterfaceOrientation
的调用委托给当前选定的子控制器。使用此扩展,您不再需要为UITabBarController创建子类,您可以像预期的那样在控制器中使用
shouldAutorotateToInterfaceOrientation

UITabBarController+Autorotate.h:

#import <UIKit/UIKit.h>

@interface UITabBarController (Autorotate)
@end

我已经能够使用它一段时间了(从我的应用程序的选项卡栏控制器),没有问题:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
     return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
这样,在适当的VC中,我们就可以进行真正的检查,在本例中是针对照片库视图(还有什么?):

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

唉,我刚刚发现,当VC潜伏在MoreViewController中时(与四个主选项卡相反),这项功能就不能很好地工作。在这种情况下,我的画廊VC从未接到过电话。我想这是因为我一直在调用的VC实际上是所选选项卡中的导航控制器,然后将内容传播到适当的VC,在本例中是我的照片库VC。但是对于更多的VC来说,事情并不是那么顺利。。。aa然后事情就从那里开始旋转下坡了\


我尝试使用Andreas的修改(见本帖其他部分),但没有效果。欢迎光临

我遇到了与您在使用UITabBarController时相同的问题。我需要控制哪些UIViewController允许旋转,哪些不允许旋转。我的主要问题是“更多”选项卡。我不希望“更多”选项卡中包含的任何UIViewController旋转

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

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

    return NO;
}
@interface MyTabBarController : UITabBarController <UITabBarDelegate> {

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

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

如果((控制器==[self-moreNavigationController])| |([self-selectedIndex]==4))处理此问题

现在,当我运行应用程序时,“更多”选项卡中的UIViewController不会旋转。我希望这将有助于其他开发人员遇到与我相同的问题


埃米利奥

谢谢,谢谢,谢谢。这已经花了两天的时间来研究如何做到这一点。当您使用带有NavigationController的tabBarController时,下面是我对您所有伟大帮助的看法

-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation{

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

     return NO;
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; 
}


对neophite编码器代码的任何批评都是值得赞赏的……jack将UITabBarController子类化(如上面公认的答案所建议的)真的可以吗

我知道苹果说的是“你永远都不应该将UITabBarController或UINavigationController子类化”——或者我误解了


无论如何;我找到了UIViewController的子类,在其中放置了一个UIAbbarController。

根据我在这里和其他地方看到的情况,我拼凑了一个解决方案,该解决方案使用了
shouldAutorotate
方法,因为旧的
shouldAutorotateToInterfaceOrientation
已被弃用

我已经把它放在UITabBarController的一个类别中我希望这是可以接受的

// 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];
}

里奇,非常感谢你。建议的解决方案很有魅力;-)这个问题快把我逼疯了!我没有意识到这样做的正确方法还需要同时检查visibleViewController。回答得好!事实上,这甚至不是必要的。UITabBarController似乎做了一些类似的事情,但只同意方向,每个选项卡的每个控制器都支持方向。Andreas,这将是一个好消息!但我在我的应用程序中没有看到这一点。在我的例子中,除非我沿着请求移动到视图控制器,否则旋转请求永远不会通过。一旦我把它放进去,首先会询问选项卡栏控制器,然后我就可以传播到VC。如果我遗漏了什么,请告诉我!事实上,当VC位于选项卡栏的“更多”部分时,我仍然遇到问题。在这种情况下,选项卡栏控制器甚至从未被调用:(我有一个电话号码