如何控制TabBar显示与隐藏

 

1.为需要观察的UITabBarController添加观察者

 

    [tabBarController.tabBar addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
    [tabBarController.tabBar addObserver:self forKeyPath:@"hidden" options:NSKeyValueObservingOptionNew context:nil];


2.实现代理方法

 

 

 

 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    
}


3.现在push一个viewController,观察其断点运行轨迹,使用的是7plus模拟器

 

第一次:改变frame为NSRect: {{0, 687}, {414, 49}}

第二次:改变frame为NSRect: {{0, 687}, {414, 49}}

第三次:改变的hidden为1

 

到此完成push操作。

pop操作类似。

 

那我们怎么仿照这个来实现自定义的tabBar当不在主页自动隐藏呢?

1.自定义tabbar

 

//
//  AppDelegate.h
//  GuideLoginDemo
//
//  Created by yfc on 17/5/10.
//  Copyright © 2017年 yfc. All rights reserved.
//
#import <UIKit/UIKit.h>


typedef enum JumpType
{
    ZHUANZHANG_HOME_LOGIN = 0,
    HUIKUAN_HOME_LOGIN ,
    JIEKUAN_HOME_LOGIN ,
    
}MYJumpType;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (assign, nonatomic) BOOL isLogin;
@property (assign, nonatomic) MYJumpType jumpType;
@property (strong, nonatomic) UIView *defaultTabbar;//

@end

 

 

 

 

 

 

    //
    //自定义tabbar
    //
    _defaultTabbar = [[UIView alloc]initWithFrame:CGRectZero];
    _defaultTabbar.height = 64;
    _defaultTabbar.width = SCREEN_WIDTH_NEW;
    _defaultTabbar.top = SCREEN_HEIGHT_NEW - 64;
    _defaultTabbar.backgroundColor = [UIColor grayColor];
        [tabBarController.view addSubview:_defaultTabbar];
    
    tabBarController.tabBar.hidden = YES;

 

 

 

 

 

 

2.自定义导航控制器DefaultNavigationcontroller

 

//
//  DefaultNavigationcontrollerViewController.h
//  GuideLoginDemo
//
//  Created by yfc on 17/6/17.
//  Copyright © 2017年 yfc. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface DefaultNavigationcontroller : UINavigationController

@end

 

//
//  DefaultNavigationcontrollerViewController.m
//  GuideLoginDemo
//
//  Created by yfc on 17/6/17.
//  Copyright © 2017年 yfc. All rights reserved.
//

#import "DefaultNavigationcontroller.h"
#import "config.h"
@interface DefaultNavigationcontroller ()<UINavigationControllerDelegate>

@end

@implementation DefaultNavigationcontroller

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;{
    
    if (viewController == self.childViewControllers[0]) {
        APPLICATION.defaultTabbar.hidden = NO;
    }else{
        APPLICATION.defaultTabbar.hidden = YES;
    }
}
@end


代码很简单,实现了一个代理方法。当前展示的如果是childViewControllers的第一个ViewController就展示否则隐藏。

 

 

20180927更新:代码就不传了,因为想到了更好的解决办法

 

 

把要push的controller(vc1)作为TabBarController(tabvc1)的同级别. 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值