1

我在有 4 个标签的 iPhone 应用程序中使用标签栏控制器。我已经为这两个方向编写了代码。

问题是当我将标签从纵向视图切换到横向视图时我遇到了麻烦。例如。如果我处于 tab1 横向模式,现在我切换标签并移动到 tab2 横向模式,但我可以看到该特定视图的纵向设置,而不是横向视图。

到目前为止,包含标签栏控制器的启动视图控制器上的代码如下:

-(void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    if (self.interfaceOrientation==UIInterfaceOrientationLandscapeLeft)
    {
        [self rotatingLandscape];
    }
    else if (self.interfaceOrientation==UIInterfaceOrientationLandscapeRight)
    {
        [self rotatingLandscape];
    }
    if (self.interfaceOrientation==UIInterfaceOrientationPortrait)
    {
        [self RotatingPotrait];
    }
    else if (self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
    {
        [self RotatingPotrait];
    }
    [super viewWillAppear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation==UIInterfaceOrientationPortrait) 
    { 
        rotate=YES; 
        [self RotatingPotrait];
    }
    if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
    { 
        rotate=YES;
        [self RotatingPotrait];
    }
    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) 
    {
        rotate=NO;
        [self rotatingLandscape]; 
    }
    if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
        rotate=NO;
        [self rotatingLandscape]; 
    }
    return YES;
}

-(void)rotatingLandscape
{
    int selIndex = [self.tabBarController selectedIndex];
    if (selIndex == 0)
    {
         [view1p rotatingLandscape];
    }
    else if (selIndex == 1)
    {
        [view2 rotatingLandscape];
    }
    else if (selIndex == 2)
    {
        [view3 rotatingLandscape];
    }
    else if (selIndex == 3)
    {
        [view4 rotatingLandscape];
    }
    self.tabBarController.view.frame=CGRectMake(0, 0, 480, 300);
}

-(void)RotatingPotrait
{
    int selIndex = [self.tabBarController selectedIndex];
    if (selIndex == 0)
    {
        [view1p RotatingPotrait];
    }
    else if (selIndex == 1)
    {
        [view2 RotatingPotrait];
    }
    else if (selIndex == 2)
    {
        [view3 RotatingPotrait];
    }
    else if (selIndex == 3)
    {
        [view4 RotatingPotrait];
    }
    self.tabBarController.view.frame=CGRectMake(0, 0, 320, 460);
   }
4

1 回答 1

1

我已经解决了我的项目支持 iOS 6.0+ 的问题 步骤:

1 创建 UITabBarViewController 的子类并添加

- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

2 子类 UINavigationController 添加

- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

3 到 AppDelegate 添加

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;

    if(self.window.rootViewController){
        UIViewController *presentedViewController = [[[[(UINavigationController *)self.window.rootViewController viewControllers] objectAtIndex:self.tabBarController.selectedIndex] viewControllers] lastObject];
        orientations = [presentedViewController supportedInterfaceOrientations];
    }

    return orientations;
}

4 给每个UIViewController添加self.tabBarCotroller.delegate = self;

和委托方法

- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController {
    NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];
    NSLog(@"Tab index = %u ", indexOfTab);

    UIViewController *mVC = [[UIViewController alloc] init];
    mVC.view.backgroundColor = [UIColor redColor];
    mVC.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:mVC animated:NO completion:nil];
    [self dismissViewControllerAnimated:NO completion:nil];
}

5 给每个 UIViewController 添加你需要的方向

- (BOOL)shouldAutorotate
{
    if (self.interfaceOrientation==UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation==UIInterfaceOrientationLandscapeRight)
    {
        return NO;

    } else {
        return YES;
    }
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape; //UIInterfaceOrientationMaskPortraite
}

对我来说这是工作

我创建了示例

于 2013-07-18T20:49:51.660 回答