2011-06-14 41 views
3

我有一個iPhone應用程序,我在2個標籤欄之間切換。問題是,在交換標籤欄的視圖中,我的標籤欄按鈕僅在第一次加載視圖(調用loadView和viewDidLoad時)時被禁用。換句話說,如果我導航到該視圖,標籤欄按鈕被禁用。如果我從該視圖導航到某些內容並彈出,則啓用按鈕。在視圖的生命週期調用的區別是:爲什麼我第一次加載我的視圖控制器時禁用了TabBar按鈕?

工作 - 的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]; 
} 

任何想法,爲什麼這些按鈕可以被禁用,所以我可以嘗試一些其他的調試,將不勝感激。有點在這裏碰壁

回答

3

如果你的包裝視圖更小,肯定會導致問題。特別是如果包裝比其內部的所有UI元素都小。 UIViews仍然可以顯示超出其框架的元素,但用戶交互不會超出其框架。

若要查看是否出現此問題,請嘗試在包裝器上將「剪貼子視圖」的屬性設置爲YES,並查看是否仍然可以看到工具欄。如果你看不到工具欄,那麼應該將包裝器做得更大,以便整個工具欄可以進行用戶交互。

+0

確實如此。我在上面指出,在失敗的情況下,包裝器的長度比我的工具欄大小縮短了49px。我需要知道的是爲什麼會發生這種情況,所以我可以修復它。我不知道如何計算包裝視圖尺寸,但它的父母是全屏,所以我不知道爲什麼不會。 – Tony 2011-07-27 15:11:58

+0

你試過在viewDidLoad中調用hideTabBarController嗎? – zachzurn 2011-07-27 17:21:03

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

} 

你試過嗎? YOu應該從兩種方法中刪除動畫,當然...

這樣我們就嵌套了兩個動畫......

OLD答:

我不知道這會幫助,但你爲什麼不嘗試隱藏/顯示子視圖設置hidden財產?

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

最後,如果這也不行,一種黑客的(但我不知道它是否工作)可能是以下幾點:做掉在viewWillAppear但不按順序叫hideTabBarControllershowCaptureTabBar;而嘗試並用0延遲調度用於執行第二方法(這意味着hideTabBarController被執行,那麼當控制流回來​​到主循環,showCaptureTabBar執行):

​​

實際上,由於hideTabBarController動畫持續0.3秒,您可以指定0.3的延遲,以防萬一因爲兩個動畫互相干擾而無法使用。

+0

我目前的做法是在非常小的延遲後顯示,但IMO不是真正的解決方案,而是一種破解。 – Tony 2011-07-25 20:38:54

+0

事實上,我自己用「hack」這個詞來表示這個......看我的編輯,也是 – sergio 2011-07-25 20:52:39

+0

這並不能解決問題。只有延遲 – Tony 2011-07-27 14:55:01

相關問題