首页 文章

使用tabBar控制器锁定到纵向的iOS旋转

提问于
浏览
1

目前正在处理使用标签栏控制器的应用程序 . 应用程序根本不会旋转到横向模式 - 所有视图都从baseVieController继承,在这里我实现了:

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

现在我知道tabBar控制器不会旋转,除非它的所有子视图都支持视图试图旋转到的方向 - 我的问题是:如果我没有在所有子视图中实现 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation方法,是否会将这些子视图锁定为纵向模式,即使我没有将其指定为所需的方向?因此将整个tabBar控制器锁定为纵向 . 我之前已经问过类似的问题,但我找不到这个具体问题的答案 . 提前致谢 .

3 回答

  • 0

    您可以旋转视图,只需要像下面一样过度骑行:只需在视图控制器类中添加要旋转的代码(这里是“SampleClassName”)

    @interface UITabBarController (rotation)
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
    @end
    
    
    @implementation UITabBarController (rotation)
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    
        if ([self.selectedViewController isKindOfClass:[UINavigationController class]])
        {
            UINavigationController *navController = (UINavigationController *) self.selectedViewController;
            if ([[navController visibleViewController] isKindOfClass:[SampleClassName class]])
                return YES;
        }
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    @end
    
  • 4

    如果您正在使用故事板为IOS 5进行开发,这将有助于我遇到同样的问题 . 通常在故事板之前,我们可能会将TabBarController添加到uiview或appdelegate中 . 使用故事板,故事板视图并不总是必须连接到视图控制器 .

    要解决这个问题

    在子类字段类型 UITabBarController 中添加新的类文件 objective-c class

    1 - 在故事板中选择选项卡栏控制器视图

    2 - 在自定义类更改 UITabBarController 到新创建的类名称后,我调用了我的MainTabBarViewController

    3 - 在你新创建的课程中改变这个

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

    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    {
            if (interfaceOrientation == UIInterfaceOrientationPortrait)
                return YES;
    
            if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
                return YES;
    
            return NO;
    }
    

    基本上发生的事情是你在Interface Builder中创建一个结构,但这只会让你成为一部分 . 在这种情况下,您仍然需要创建伴随代码 . 这让我很困惑,因为我习惯于使用.xib从头开始构建视图,并且通常会从appdelegate配置tabbarcontroller .

    你也可以有条件地控制这些

    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
        return NO;
    
    if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
        return NO;
    
    if (interfaceOrientation == UIInterfaceOrientationPortrait)
        return YES;
    
    if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        return YES;
    

    只需为你想要的东西返回yes,为你不想要的东西返回no . 或接受一切返回是 .

  • 1

    至于你的父视图控制器(在你的情况下 - baseViewController)实现这个方法 -

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

    您无需在子视图控制器中实现此功能,因为它支持所有子视图控制器中的所有方向 .

相关问题