全国旗舰校区

不同学习城市 同样授课品质

北京

深圳

上海

广州

郑州

大连

武汉

成都

西安

杭州

青岛

重庆

长沙

哈尔滨

南京

太原

沈阳

合肥

贵阳

济南

下一个校区
就在你家门口
+
当前位置:首页  >  技术干货

iOS隐藏TabBar的实现方法

发布时间:2023-11-25 04:54:59
发布人:xqq

在iOS开发中,有很多时候我们需要隐藏TabBar,比如进入某个页面后不需要TabBar等。下面就从多个方面来讲解iOS隐藏TabBar的实现方法。

一、利用系统自带的方法

iOS系统提供了一个属性来控制TabBar的隐藏和显示:hidesBottomBarWhenPushed。可以在进入下一个页面前,将该属性设置为YES即可隐藏TabBar:

UIViewController *vc = [[UIViewController alloc] init];
vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];

需要注意的是,该方法只适用于跳转到下一个页面时隐藏TabBar,如果需要在同页面内隐藏TabBar,则需要采用其他方法。

二、手动隐藏TabBar

当需要在同一页面内隐藏TabBar时,可以手动隐藏,具体实现代码如下:

- (void)hideTabBar {
    UIView *contentView = self.view;
    CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
    for (UIView *aView in contentView.subviews) {
        if ([aView isKindOfClass:[UITabBar class]]) {
            [UIView animateWithDuration:0.3 animations:^{
                CGRect frame = aView.frame;
                frame.origin.y = screenHeight;
                aView.frame = frame;
            }];
        } else {
            [UIView animateWithDuration:0.3 animations:^{
                CGRect frame = aView.frame;
                if (UIScreen.mainScreen.bounds.size.height == 812) {
                    frame.size.height = screenHeight - kTopHeight - kSafeBottomHeight;
                } else {
                    frame.size.height = screenHeight - kTopHeight - kTabBarHeight;
                }
                aView.frame = frame;
            }];
        }
    }
}

在需要隐藏TabBar的时候,调用hideTabBar方法即可。

三、自定义TabBar实现隐藏

有时候需要实现自定义的TabBar效果,并且在某些页面需要隐藏TabBar。这时候可以通过自定义TabBar来实现,具体步骤如下:

Step 1:创建自定义TabBar

创建自定义的TabBar,可以继承系统的UITabBar类,实现自己的TabBar样式。

// MyTabBar.h文件
#import 

@protocol MyTabBarDelegate 

- (void)centerButtonDidClick;

@end

@interface MyTabBar : UITabBar

@property (nonatomic, weak) id myDelegate;

@end

// MyTabBar.m文件
#import "MyTabBar.h"

@implementation MyTabBar

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    UIButton *centerButton = [[UIButton alloc] init];
    [centerButton setImage:[UIImage imageNamed:@"tab_center"] forState:UIControlStateNormal];
    [centerButton sizeToFit];
    [centerButton addTarget:self action:@selector(centerButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:centerButton];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    int index = 0;
    CGFloat buttonW = self.bounds.size.width / 5;
    CGFloat buttonH = self.bounds.size.height;
    CGFloat buttonY = 0;
    CGFloat centerX = self.bounds.size.width / 2;
    for (UIView *subview in self.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            CGFloat buttonX = buttonW * (index > 1 ? index + 1 : index);
            subview.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
            index++;
            
            if (index == 2) {
                index++;
            }
        }
    }
    
    UIButton *centerButton = self.subviews[4];
    centerButton.center = CGPointMake(centerX, centerButton.bounds.size.height / 2.0);
}

- (void)centerButtonClicked {
    if ([self.myDelegate respondsToSelector:@selector(centerButtonDidClick)]) {
        [self.myDelegate centerButtonDidClick];
    }
}

@end

Step 2:创建自定义TabBarController

创建自定义的TabBarController,继承系统的UITabBarController类,并在其viewWillAppear方法中隐藏TabBar。

// MyTabBarController.h文件
#import 
#import "MyTabBar.h"

@interface MyTabBarController : UITabBarController

@end

// MyTabBarController.m文件
#import "MyTabBarController.h"

@interface MyTabBarController ()

@property (nonatomic, strong) MyTabBar *myTabBar;

@end

@implementation MyTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    
    // 创建自定义TabBar
    MyTabBar *tabBar = [[MyTabBar alloc] init];
    tabBar.myDelegate = self;
    [self setValue:tabBar forKey:@"tabBar"]; // 使用KVC替换系统的TabBar
    self.myTabBar = tabBar;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // 隐藏系统TabBar
    for (UIView *view in self.tabBar.subviews) {
        view.hidden = YES;
    }
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    return viewController != self.selectedViewController;
}

- (void)centerButtonDidClick {
    NSLog(@"Center Button Did Click.");
}

@end

使用自定义的TabBarController,并需要在某些页面隐藏TabBar时,只需要调用以下方法即可:

self.myTabBar.hidden = YES;

四、使用第三方库实现隐藏TabBar

在iOS开发中有很多第三方库可以用来隐藏TabBar,比如TabbarHide、DDHTabBar等。这里以TabbarHide为例:

Step 1:安装TabbarHide

pod 'TabbarHide', '~> 0.0.3'

Step 2:实现TabbarHide

在需要隐藏TabBar的时候,调用以下方法即可:

[self.tabBarController setTabBarHidden:YES animated:YES];

同样的,如果需要显示TabBar,也可以调用以下方法:

[self.tabBarController setTabBarHidden:NO animated:YES];

五、总结

以上便是iOS隐藏TabBar的实现方法,可以根据具体需求选择合适的方法来实现。如果需要在同一页面内隐藏TabBar,可以手动隐藏或自定义TabBar;如果需要在切换到下一个页面时隐藏TabBar,可以利用系统自带的方法;如果需要使用已有的第三方库,可以使用TabbarHide等相关库。

kubectlgetsvc

相关文章

React中的nextTick函数

React中的nextTick函数

2023-11-25
如何控制echartsx轴字体大小

如何控制echartsx轴字体大小

2023-11-25
如何在 PHP 7.4 上进行安装

如何在 PHP 7.4 上进行安装

2023-11-25
深入了解loggerfactory

深入了解loggerfactory

2023-11-25

最新文章

武汉新媒体行业公司排名

武汉新媒体行业公司排名

2023-11-01
武汉新媒体就业现状好吗

武汉新媒体就业现状好吗

2023-11-01
武汉全媒体行业发展现状及趋势

武汉全媒体行业发展现状及趋势

2023-10-31
武汉全媒体现状

武汉全媒体现状

2023-10-31