在推动UIView时隐藏UITabBar

时间:2009-03-24 01:42:18

标签: iphone uitabbarcontroller uinavigationbar

我有一个UITabBarController,默认视图控制器是UINavigationController。我希望能够在UINavigationController推送某个视图时隐藏UITabBarController的UITabBar。

我尝试添加:

delegate.tabBarController.hidesBottomBarWhenPushed = YES;
在我推动视图之前,在我的UINavigationController

,但这似乎没有办法。

关于我应该做什么或者是否可能的任何提示?提前谢谢!

9 个答案:

答案 0 :(得分:96)

这样更好:

viewController.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:viewController animated:YES];

您必须在要进入视图的控制器上设置hidesBottomBarWhenPushed = YES ...

答案 1 :(得分:4)

使用故事板时,它易于设置的视图控制器将在推送时隐藏标签栏,在目标视图控制器上只需选中此复选框:
enter image description here

答案 2 :(得分:3)

我已经弄清楚如何解决这个问题,我遇到了同样的问题,但Apple也告诉我们如何在名为“The Elements”(http://developer.apple.com/library/ios/#samplecode/TheElements/Introduction/Intro.html

请参阅下面有关如何操作的功能,将此添加到您要推入的视图的init函数中!

-(id) init { 
    if(self = [super init]) { 
        self.hidesBottomBarWhenPushed = YES; 
    } 
    return self; 
}

它会像照片应用程序在iPhone上自动隐藏标签栏。当您向后导航时,父视图将再次显示标签栏。

祝你好运

答案 3 :(得分:3)

我尝试了大多数建议的解决方案。最后他们都没有为我工作。

hideTabBarWhenPushed不仅隐藏了下一个被推送的视图控制器的标签栏,而且还隐藏了所有被推入的视图控制器。对于那些我确实希望标签栏控制器重新出现。

Orafaelreis的解决方案(见上文)似乎最适合。但他的尝试只适用于严格的肖像取向,甚至不是颠倒。所以我不得不修补它。这就是我最终得到的:

#define kTabBarHeight               49 // This may be different on retina screens. Frankly, I have not yet tried.

- (void) hideTabBar:(BOOL)hide {

    // fetch the app delegate
    AppDelegate         *delegate   = [[UIApplication sharedApplication] delegate];

    // get the device coordinates
    CGRect              bounds      = [UIScreen mainScreen].bounds;
    float               width;
    float               height;

    // Apparently the tab bar controller's view works with device coordinates  
    // and not with normal view/sub view coordinates
    // Therefore the following statement works for all orientations. 
    width                   = bounds.size.width;
    height                  = bounds.size.height;

    if (hide) {

        // The tab bar should be hidden too. 
        // Otherwise it may flickr up a moment upon rotation or 
        // upon return from detail view controllers. 
        [self.tabBarController.tabBar setHidden:YES];

        // Hiding alone is not sufficient. Hiding alone would leave us with an unusable black
        // bar on the bottom of the size of the tab bar. 
        // We need to enlarge the tab bar controller's view by the height of the tab bar. 
        // Doing so the tab bar, although hidden, appears just beneath the screen. 
        // As the tab bar controller's view works in device coordinations, we need to enlarge 
        // it by the tab bar height in the appropriate direction (height in portrait and width in landscape)
        // and in reverse/upside down orientation we need to shift the area's origin beyond zero. 
        switch (delegate.tabBarController.interfaceOrientation) {
            case UIInterfaceOrientationPortrait:
                // Easy going. Just add the space on the bottom.
                [self.tabBarController.view setFrame:CGRectMake(0,0,width,height+kTabBarHeight)];
                break;

            case UIInterfaceOrientationPortraitUpsideDown:
                // The bottom is now up! Add the appropriate space and shift the rect's origin to y = -49
                [self.tabBarController.view setFrame:CGRectMake(0,-kTabBarHeight,width,height+kTabBarHeight)];
                break;

            case UIInterfaceOrientationLandscapeLeft:
                // Same as Portrait but add the space to the with but the height
                [self.tabBarController.view setFrame:CGRectMake(0,0,width+kTabBarHeight,height)];
                break;

            case UIInterfaceOrientationLandscapeRight:
                // Similar to Upside Down: Add the space and shift the rect. Just use x and with this time
                [self.tabBarController.view setFrame:CGRectMake(0-kTabBarHeight,0,width+kTabBarHeight,height)];
                break;

            default:
                break;
        }
    } else {
        // reset everything to its original state. 
        [self.tabBarController.view setFrame:CGRectMake(0,0,width,height)];
        [self.tabBarController.tabBar setHidden:NO];
    }

    return; 
}


- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{

    // It is important to call this method at all and to call it here and not in willRotateToInterfaceOrientation
    // Otherwise the tab bar will re-appear. 
    [self hideTabBar:YES];

    // You may want to re-arrange any other views according to the new orientation
    // You could, of course, utilize willRotateToInterfaceOrientation instead for your subViews. 
}

- (void)viewWillAppear: (BOOL)animated { 

    // In my app I want to hide the status bar and navigation bar too. 
    // You may not want to do that. If so then skip the next two lines. 
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];

    [self hideTabBar: YES];

    // You may want to re-arrange your subviews here. 
    // Orientation may have changed while detail view controllers were visible. 
    // This method is called upon return from pushed and pulled view controllers.   

    return;
}

- (void)viewWillDisappear: (BOOL)animated {     

    // This method is called while this view controller is pulled
    // or when a sub view controller is pushed and becomes visible
    // Therefore the original settings for the tab bar, navigation bar and status bar need to be re-instated

    [self hideTabBar:NO];

    // If you did not change the appearance of the navigation and status bar in viewWillAppear,
    // then you can skip the next two statements too. 
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];

    return;
}

内联评论应该解释每个陈述的推理。虽然,可能有更聪明的编码方式。

与隐藏状态栏和导航栏一起有一个副作用,我不想向你们隐瞒。 1.从此导航控制器返回到主叫导航控制器时,调用控制器上的状态栏和导航栏会重叠,直到设备旋转一次或直到另一个选项卡出现后再次选择相关选项卡。 2.当调用视图控制器是一个表视图,并且当设备在返回到表格时处于横向模式时,表格将以适当的横向方向显示,但它的布局就像是纵向一样。左上角很好,但屏幕下方隐藏了一些表格单元格和标签栏。在右手边有一些自由空间。这也是通过再次旋转设备来解决的。

一旦我找到了解决这些轻微但令人讨厌的错误的解决方案,我会及时通知您。

答案 4 :(得分:2)

以下是您如何使用它:

Application Delegate中创建UITabBarController。然后创建一个UINavigationController,其根控制器作为特定选项卡中所需的视图控制器。然后将UINavigationController插入UITabBarController的“ viewControllers ”数组中。像这样:

ViewControllerForTab1 *tab1Controller = [[ViewControllerForTab1 alloc] initWithNibName:@"ViewControllerForTab1"];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tab1Controller];

[tab1Controller release];


UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects: navController, nil];

[navController release];


[self.window addSubView:tabBarController.view];

这样,您可以在hidesBottomBarWhenPushed内的任何视图控制器中将“YES”属性设置为“UINavigationController”,它将隐藏UITabBar

希望有所帮助!

答案 5 :(得分:1)

我会在这里让我的解决方案:

#define FRAME_HIDDEN CGRectMake(0, 0, 768, 1073) //1073 = 1024 (screen) + 49 (UITabBar) 
#define FRAME_APPEAR CGRectMake(0, 0, 768,1024)

-(void) setHidden: (BOOL) hidden{
    CGRect frame = (hidden)? FRAME_HIDDEN : FRAME_APPEAR;
    [self.tabBarController.view setFrame:frame];
    [self.tabBarController.tabBar setHidden:hidden];
}

在您需要的地方调用'setHidden'方法!我使用这个和'Singleton Pattern',然后我的子视图可以在他的Superview中隐藏UITabBar

答案 6 :(得分:0)

事实证明,如果您设置视图hidesBottomBarWhenPushed:YES,它会在视图出现时隐藏条形图(对我而言)。我把它分配给了UITabBarController,当你想到它时,它没有多大意义。

[self.view hidesBottomBarWhenPushed:YES];
[super pushViewController:viewController animated:animated];

答案 7 :(得分:0)

在第一个UIViewController" FirstItemViewController"

 @IBAction func pushToControllerAction(sender: AnyObject) {
     self.hidesBottomBarWhenPushed = true
     self.performSegueWithIdentifier("nextController", sender: self)
 }

在下一个UIViewController" ExampleViewController"`

 override func willMoveToParentViewController(parent: UIViewController?) {
         if parent == nil {
             var viewControllers = self.navigationController!.viewControllers
             if ((viewControllers[viewControllers.count - 2]).isKindOfClass(FirstItemViewController.self)) {
                 (viewControllers[viewControllers.count - 2] as! FirstItemViewController).hidesBottomBarWhenPushed = false
             }
         }
 }

看看这个答案https://stackoverflow.com/a/36148064/3078925

答案 8 :(得分:0)

在要隐藏的控制器中使用hidesBottomBarWhenPushed

用于隐藏放入prepare for segue的所有控制器

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    segue.destination.hidesBottomBarWhenPushed = true
}