隐藏UITabbarController

来源:互联网 发布:杨幂爱的供养 知乎 编辑:程序博客网 时间:2024/05/20 23:32

平时我们需要自定义UITabBarController,则需要隐藏系统自带的.

如果使用

 [self.tabBarController.tabBarsetHidden:YES];

这个方法就会有个问题-出现白条。

那么就需要我们为UITabBarController添加一个类别

@interface UITabBarController (HideTabBar)

在.h里面添加两个方法:

+(void)hidden:(UITabBarController *)tabbarcontroller isHidden:(BOOL)hidden;

+(void)hiddenTabBar:(UITabBarController *)tabbarcontroller animated:(BOOL)animated isHidden:(BOOL)isHidden

;

在.m文件里实现两个方法:

+(void)hidden:(UITabBarController *)tabbarcontroller isHidden:(BOOL)hidden

{

   CGRect frame=[UIScreenmainScreen].bounds;

    

    for(UIView *view in tabbarcontroller.view.subviews)

    {

        if([view isKindOfClass:[UITabBarclass]])

        {

            if (hidden) 

            {

                [viewsetFrame:CGRectMake(view.frame.origin.x, frame.size.height+16, view.frame.size.width, view.frame.size.height)];

            }

            else

            {

                [viewsetFrame:CGRectMake(view.frame.origin.x, frame.size.height-49, view.frame.size.width, view.frame.size.height)];

            }

        }

        else

        {

            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, frame.size.height)];

        }

        

    }

    

}


+(void)hiddenTabBar:(UITabBarController *)tabbarcontroller animated:(BOOL)animated isHidden:(BOOL)isHidden

{

    if (animated) {

        [UIViewbeginAnimations:nilcontext:NULL];

        [UIViewsetAnimationDuration:0.3];

        

        [selfhidden:tabbarcontroller isHidden:isHidden];

        

        [UIViewcommitAnimations];

    }

    else{

        [selfhidden:tabbarcontroller isHidden:isHidden];

    }

}


需要在哪个视图隐藏它就在哪加入头文件:

#import"UITabBarController+HideTabBar.h"

在代码行使用类方法调用

[UITabBarControllerhiddenTabBar:self.tabBarControlleranimated:YESisHidden:NO];




原创粉丝点击