2011-06-02 30 views
3

如何在隐藏UITabBar的时候添加观察器(通过'hides-bottom-bar-when-pushing')?我有一个自定义按钮,位于我的标签栏下方,我想确保它在隐藏UITabBar时不会出现。谢谢!UITabBar从代理隐藏时观察

回答

1

的最好的选择是把你的UIToolbar在启用了剪辑的UIView中,并将剪辑视图放置在UITabBar的上方。然后添加这个UIView作为您的UITabBar的子视图。这种方式显示和隐藏UITabBar将自动显示或隐藏您的UIToolbar现在您可以动画显示和隐藏您的UIToolbar,并且每次UITabBar时仍然会消失。

+0

UITabBarController中的tabbar是只读的,你怎么能在它上面添加一个子视图? – user392412 2012-02-24 06:43:19

3

尝试使用UINavigationControllerDelegate protocol

- (void)navigationController:(UINavigationController *)navigationController 
     willShowViewController:(UIViewController *)viewController 
        animated:(BOOL)animated 
{ 
    if (viewController.hidesBottomBarWhenPushed) { 
     // ... 
    } 
} 
-1

这会告诉你的时候该字段的值更改:

UITabBar *myTabBar = [[UITabBar alloc] init]; 

[self addObserver:myInterestedObjectWhoWantsToKnowWhenTabBarHiddenChanges 
     forKeyPath:@"myTabBar.hidesBottomBarWhenPushed" 
      options:NSKeyValueObservingOptionNew 
      context:nil]; 

然后在myInterestedObjectWhoWantsToKnowWhenTabBarHiddenChanges.m,实现

 - (void)observeValueForKeyPath:(NSString *)keyPath 
        ofObject:(id)object 
        change:(NSDictionary *)change 
        context:(void *)context {  

       if ([keyPath isEqualToString:@"myTabBar.hidesBottomBarWhenPushed"]) { // this key must match, where observer is set.   
        // object will be "self" from the code above 
        // and the change dictionary will have the old and new values. 
       } 
     } 
+0

实际上不起作用。 – Andy 2015-04-10 15:38:49