为什么第一次加载视图控制器时我的TabBar按钮被禁用?

时间:2011-06-14 12:31:35

标签: ios uitabbarcontroller

我有一个iPhone应用程序,我在两个标签栏之间切换。问题是在我交换标签栏的视图中,我的标签栏按钮仅在第一次加载视图时被禁用(当调用loadView和viewDidLoad时)。换句话说,如果我导航到该视图,则禁用选项卡栏按钮。如果我从该视图导航到某个内容然后弹回,则启用按钮。视图生命周期调用的不同之处是:

工作 - 的loadView viewDidLoad中 viewWillAppear中 viewDidAppear

不工作 - viewWillAppear中 viewDidAppear

现在交换在viewDidAppear中。如果我将交换置于viewWillAppear中,则按钮工作正常但视图控制器会跳转,因为在视图转换动画期间完成了标签栏交换动画。

我的第一个猜测是某些视图位于标签栏的顶部。我不认为这是真的。我在两个场景中打印出了视图层次结构,但只有一个小区别。

以下是工作方案的视图层次结构:

<UIWindow: 0x5a4c7c0; frame = (0 0; 320 480); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x5a4ca30>>
   | <UILayoutContainerView: 0x5a591b0; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x5a59230>>
   |    | <UITransitionView: 0x5a59c00; frame = (0 0; 320 480); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x5a59ca0>>
   |    |    | <UIViewControllerWrapperView: 0x5a6e960; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x5a6e9e0>>
   |    |    |    | <UILayoutContainerView: 0x5a5bb70; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x5a5bbc0>>
   |    |    |    |    | <UINavigationTransitionView: 0x5a5c290; frame = (0 0; 320 480); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x5a19400>>
   |    |    |    |    |    | <UIViewControllerWrapperView: 0xaf01810; frame = (0 20; 320 460); layer = <CALayer: 0xaf01840>>
   |    |    |    |    |    |    | <UIView: 0x5abcba0; frame = (0 0; 320 460); layer = <CALayer: 0x5a8ff50>>
   |    |    |    |    |    |    |    | <CaptureAuxMenu: 0x5ac6850; frame = (0 480; 320 49); layer = <CALayer: 0x5ac68e0>>
   |    |    |    |    |    |    |    |    | <UIImageView: 0x5ac6a50; frame = (0 0; 320 49); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5ac6a80>>
   |    |    |    |    |    |    |    |    | <UIButton: 0x5ac67c0; frame = (18 7; 80 31); opaque = NO; layer = <CALayer: 0x5ac6b80>>
   |    |    |    |    |    |    |    |    |    | <UIImageView: 0x5acf910; frame = (0 0; 80 31); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5acf940>>
   |    |    |    |    |    |    |    |    | <UIButton: 0x5ac7ed0; frame = (116 7; 80 31); opaque = NO; layer = <CALayer: 0x5ac7b50>>
   |    |    |    |    |    |    |    |    |    | <UIImageView: 0x5acf890; frame = (0 0; 80 31); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5acf8c0>>
   |    |    |    |    |    |    |    |    | <UIButton: 0x5ac91f0; frame = (285 10; 25 25); opaque = NO; layer = <CALayer: 0x5ac8e70>>
   |    |    |    |    |    |    |    |    |    | <UIImageView: 0x5acf810; frame = (0 0; 25 25); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5acf840>>

   |    | <UITabBar: 0x5a59560; frame = (0 431; 320 480); hidden = YES; autoresize = W+TM; layer = <CALayer: 0x5a59600>>
   |    |    | <UITabBarButton: 0x5a638f0; frame = (2 1; 60 479); opaque = NO; layer = <CALayer: 0x5a660a0>>
   |    |    |    | <UITabBarSelectionIndicatorView: 0x5a6e250; frame = (0 2; 60 475); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a6e280>>
   |    |    |    | <UITabBarSwappableImageView: 0x5a662a0; frame = (6 218; 48 32); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a66360>>
   |    |    |    | <UITabBarButtonLabel: 0x5a63ba0; frame = (16 465; 28 13); text = 'string'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a63990>>
   |    |    | <UITabBarButton: 0x5a660d0; frame = (66 1; 60 479); opaque = NO; layer = <CALayer: 0x5a674a0>>
   |    |    |    | <UITabBarSwappableImageView: 0x5a663c0; frame = (6 218; 48 32); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a66410>>
   |    |    |    | <UITabBarButtonLabel: 0x5a66cc0; frame = (16 465; 28 13); text = 'string'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a665e0>>
   |    |    | <UITabBarButton: 0x5a65990; frame = (130 1; 60 479); opaque = NO; layer = <CALayer: 0x5a68140>>
   |    |    |    | <UITabBarSwappableImageView: 0x5a689c0; frame = (6 218; 48 32); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a68170>>
   |    |    |    | <UITabBarButtonLabel: 0x5a696b0; frame = (16 465; 28 13); text = 'string'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a68ba0>>
   |    |    | <UITabBarButton: 0x5a68420; frame = (194 1; 60 479); opaque = NO; layer = <CALayer: 0x5a697d0>>
   |    |    |    | <UITabBarSwappableImageView: 0x5a6abf0; frame = (6 218; 48 32); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a6ac40>>
   |    |    |    | <UITabBarButtonLabel: 0x5a6af20; frame = (8 465; 44 13); text = 'FRIENDS'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a678c0>>
   |    |    | <UITabBarButton: 0x5a6a5a0; frame = (258 1; 60 479); opaque = NO; layer = <CALayer: 0x5a6b230>>
   |    |    |    | <UITabBarSwappableImageView: 0x5a6b9b0; frame = (6 218; 48 32); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a6b260>>
   |    |    |    | <UITabBarButtonLabel: 0x5a677c0; frame = (8 465; 44 13); text = 'PROFILE'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a6b3d0>>
   |    | <UIImageView: 0x5a6b850; frame = (0 480; 320 49); opaque = NO; tag = 89364833; layer = <CALayer: 0x5a6d200>>
   |    |    | <UIButton: 0x5a6d580; frame = (0 0; 64 49); opaque = NO; layer = <CALayer: 0x5a6d660>>
   |    |    |    | <UIImageView: 0x5c35b20; frame = (0 0; 63 49); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5c35b50>>
   |    |    | <UIButton: 0x5a63480; frame = (64 0; 64 49); opaque = NO; tag = 1; layer = <CALayer: 0x5a66170>>
   |    |    |    | <UIImageView: 0x5c35aa0; frame = (0 0; 64 49); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5c35ad0>>
   |    |    | <UIButton: 0x5a6da30; frame = (128 0; 64 49); opaque = NO; tag = 2; layer = <CALayer: 0x5a6dac0>>
   |    |    |    | <UIImageView: 0x5c35a20; frame = (0 0; 64 49); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5c35a50>>
   |    |    | <UIButton: 0x5a6dc60; frame = (192 0; 64 49); opaque = NO; tag = 3; layer = <CALayer: 0x5a6dcf0>>
   |    |    |    | <UIImageView: 0x5c359a0; frame = (0 0; 64 49); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5c359d0>>
   |    |    | <UIButton: 0x5a6de90; frame = (256 0; 64 49); opaque = NO; tag = 4; layer = <CALayer: 0x5a6df20>>
   |    |    |    | <UIImageView: 0x5c358c0; frame = (0 0; 64 49); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5c35970>>

以下是失败方案的视图层次结构:

<UIWindow: 0x5a4c7c0; frame = (0 0; 320 480); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x5a4ca30>>
   | <UILayoutContainerView: 0x5a591b0; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x5a59230>>
   |    | <UITransitionView: 0x5a59c00; frame = (0 0; 320 480); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x5a59ca0>>
   |    |    | <UIViewControllerWrapperView: 0x5a6e960; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x5a6e9e0>>
   |    |    |    | <UILayoutContainerView: 0x5a5bb70; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x5a5bbc0>>
   |    |    |    |    | <UINavigationTransitionView: 0x5a5c290; frame = (0 0; 320 480); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x5a19400>>
   |    |    |    |    |    | <UIViewControllerWrapperView: 0x5ae49f0; frame = (0 20; 320 411); layer = <CALayer: 0x5ae4870>>
   |    |    |    |    |    |    | <UIView: 0x5abcba0; frame = (0 0; 320 411); layer = <CALayer: 0x5a8ff50>>
   |    |    |    |    |    |    |    | <CaptureAuxMenu: 0x5ac6850; frame = (0 480; 320 49); layer = <CALayer: 0x5ac68e0>>
   |    |    |    |    |    |    |    |    | <UIImageView: 0x5ac6a50; frame = (0 0; 320 49); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5ac6a80>>
   |    |    |    |    |    |    |    |    | <UIButton: 0x5ac67c0; frame = (18 7; 80 31); opaque = NO; layer = <CALayer: 0x5ac6b80>>
   |    |    |    |    |    |    |    |    |    | <UIImageView: 0x5acf910; frame = (0 0; 80 31); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5acf940>>
   |    |    |    |    |    |    |    |    | <UIButton: 0x5ac7ed0; frame = (116 7; 80 31); opaque = NO; layer = <CALayer: 0x5ac7b50>>
   |    |    |    |    |    |    |    |    |    | <UIImageView: 0x5acf890; frame = (0 0; 80 31); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5acf8c0>>
   |    |    |    |    |    |    |    |    | <UIButton: 0x5ac91f0; frame = (285 10; 25 25); opaque = NO; layer = <CALayer: 0x5ac8e70>>
   |    |    |    |    |    |    |    |    |    | <UIImageView: 0x5acf810; frame = (0 0; 25 25); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5acf840>>

   |    | <UITabBar: 0x5a59560; frame = (0 431; 320 480); hidden = YES; autoresize = W+TM; layer = <CALayer: 0x5a59600>>
   |    |    | <UITabBarButton: 0x5a638f0; frame = (2 1; 60 479); opaque = NO; layer = <CALayer: 0x5a660a0>>
   |    |    |    | <UITabBarSelectionIndicatorView: 0x5a6e250; frame = (0 2; 60 475); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a6e280>>
   |    |    |    | <UITabBarSwappableImageView: 0x5a662a0; frame = (6 218; 48 32); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a66360>>
   |    |    |    | <UITabBarButtonLabel: 0x5a63ba0; frame = (16 465; 28 13); text = 'string'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a63990>>
   |    |    | <UITabBarButton: 0x5a660d0; frame = (66 1; 60 479); opaque = NO; layer = <CALayer: 0x5a674a0>>
   |    |    |    | <UITabBarSwappableImageView: 0x5a663c0; frame = (6 218; 48 32); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a66410>>
   |    |    |    | <UITabBarButtonLabel: 0x5a66cc0; frame = (16 465; 28 13); text = 'string'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a665e0>>
   |    |    | <UITabBarButton: 0x5a65990; frame = (130 1; 60 479); opaque = NO; layer = <CALayer: 0x5a68140>>
   |    |    |    | <UITabBarSwappableImageView: 0x5a689c0; frame = (6 218; 48 32); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a68170>>
   |    |    |    | <UITabBarButtonLabel: 0x5a696b0; frame = (16 465; 28 13); text = 'string'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a68ba0>>
   |    |    | <UITabBarButton: 0x5a68420; frame = (194 1; 60 479); opaque = NO; layer = <CALayer: 0x5a697d0>>
   |    |    |    | <UITabBarSwappableImageView: 0x5a6abf0; frame = (6 218; 48 32); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a6ac40>>
   |    |    |    | <UITabBarButtonLabel: 0x5a6af20; frame = (8 465; 44 13); text = 'FRIENDS'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a678c0>>
   |    |    | <UITabBarButton: 0x5a6a5a0; frame = (258 1; 60 479); opaque = NO; layer = <CALayer: 0x5a6b230>>
   |    |    |    | <UITabBarSwappableImageView: 0x5a6b9b0; frame = (6 218; 48 32); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a6b260>>
   |    |    |    | <UITabBarButtonLabel: 0x5a677c0; frame = (8 465; 44 13); text = 'PROFILE'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5a6b3d0>>
   |    | <UIImageView: 0x5a6b850; frame = (0 480; 320 49); opaque = NO; tag = 89364833; layer = <CALayer: 0x5a6d200>>
   |    |    | <UIButton: 0x5a6d580; frame = (0 0; 64 49); opaque = NO; layer = <CALayer: 0x5a6d660>>
   |    |    |    | <UIImageView: 0x5c35b20; frame = (0 0; 63 49); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5c35b50>>
   |    |    | <UIButton: 0x5a63480; frame = (64 0; 64 49); opaque = NO; tag = 1; layer = <CALayer: 0x5a66170>>
   |    |    |    | <UIImageView: 0x5c35aa0; frame = (0 0; 64 49); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5c35ad0>>
   |    |    | <UIButton: 0x5a6da30; frame = (128 0; 64 49); opaque = NO; tag = 2; layer = <CALayer: 0x5a6dac0>>
   |    |    |    | <UIImageView: 0x5c35a20; frame = (0 0; 64 49); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5c35a50>>
   |    |    | <UIButton: 0x5a6dc60; frame = (192 0; 64 49); opaque = NO; tag = 3; layer = <CALayer: 0x5a6dcf0>>
   |    |    |    | <UIImageView: 0x5c359a0; frame = (0 0; 64 49); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5c359d0>>
   |    |    | <UIButton: 0x5a6de90; frame = (256 0; 64 49); opaque = NO; tag = 4; layer = <CALayer: 0x5a6df20>>
   |    |    |    | <UIImageView: 0x5c358c0; frame = (0 0; 64 49); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x5c35970>>

在两种情况下,我能看到的唯一值得注意的差异是在第7行和第8行。你会看到UIViewControllerWrapperView和子UIView有不同的框架,但我不太清楚为什么会出现问题,因为我不确定哪个UIView对应,我找不到UIViewControllerWrapperView上的任何文档。

我使用以下代码交换标签栏:

- (void)viewDidAppear:(BOOL)animated{ 
    [super viewDidAppear:animated];
    [AppHelpers hideTabBarController:self.tabBarController withHidden:YES];
    [self showCaptureTabBar];
}

+ (void) hideTabBarController:(UITabBarController *)tbc withHidden:(BOOL)hide {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

    for(UIView *view in tbc.view.subviews)
    {       
        if([view isKindOfClass:[UIImageView class]])
        {
            if (hide) {
                [view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
            } else {
                [view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
            }
        }else {
            if (hide) {
                // When hiding the tabbarcontroller, the view should be full height
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
            } else {
                // When showing the tabbarcontroller, the view needs to leave 49px of room for it at the bottom
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
            }
        }
    }

    [UIView commitAnimations];  
}

- (void)showCaptureTabBar {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    [captureTabBar setFrame:CGRectMake(captureTabBar.frame.origin.x, 412.0f, captureTabBar.frame.size.width, captureTabBar.frame.size.height)];
    [UIView commitAnimations];
}

关于为什么按钮可以被禁用以便我可以尝试其他调试的任何想法将非常感激。有点在这里撞墙

2 个答案:

答案 0 :(得分:3)

如果您的包装器视图较小,肯定会导致问题。特别是如果包装器小于其内部的所有UI元素。 UIViews仍然可以显示超出其框架的元素,但用户交互不会超出其框架。

要查看是否存在此问题,请尝试在包装器上将“剪辑子视图”的属性设置为“是”,然后查看是否仍可以看到工具栏。如果您看不到工具栏,那么包装器应该做得更大,以便整个工具栏可以进行用户交互。

答案 1 :(得分:0)

在单个beginAnimations / endAnimations上下文中进行隐藏/显示怎么样?

- (void)viewDidAppear:(BOOL)animated{ 
 [super viewDidAppear:animated];

 [UIView beginAnimations:@"firstAnimation" context:NULL];
 [UIView setAnimationDuration:0.3];
 [AppHelpers hideTabBarController:self.tabBarController withHidden:YES];

 [UIView beginAnimations:@"secondAnimation" context:NULL];
 [UIView setAnimationDuration:1.0];
 [self showCaptureTabBar];

 [UIView commitAnimations];
 [UIView commitAnimations];

}
你试过这个吗?你应该从这两种方法中删除动画,当然......

通过这种方式我们可以嵌套两个动画......

老答案:

我不知道这是否有帮助,但为什么不尝试隐藏/显示设置hidden属性的子视图?

        if (hide) {
            view.hide = YES;
        } else {
            view.hide = NO;
        }

最后,如果这不起作用,那么一种黑客攻击(但我不知道它是否有效)可能如下:在viewWillAppear中进行交换但不按顺序调用hideTabBarControllershowCaptureTabBar;而是尝试并安排第二个执行方法,延迟为0(这意味着执行hideTabBarController,然后当控制流返回主循环时,执行showCaptureTabBar):

[AppHelpers hideTabBarController:self.tabBarController withHidden:YES];
[self performSelector:@selector(showCaptureTabBar) withObject:nil afterDelay:0];

实际上,由于hideTabBarController动画持续0.3秒,你可以指定0.3的延迟,以防万一这不起作用,因为这两个动画相互干扰。