这两天在做新闻客户端,遇到一个问题:如何将作为根视图控制器的自定义UITabBarController在主页转到子页时隐藏
self.tabbarcontroller.tabbar.hidden = YES;
这种方法只适用于系统的tabbar
我打印出了自定义tabbarcontroller和self.tabbarcontroller地址发现不一样
认识到这种误区,我就想办法找到我自定义的tabbarcontroller
我突然想到试图控制器的根控制器就是我自定义的tabbarcontroller(下称Customtabbar)
不通过代理找根视图
通过以下方法即可实现对自定义tabbarcontroller的隐藏和显示
UIWindow * window = [UIApplication sharedApplication].keyWindow;
CustomTabBar * ct =(CustomTabBar *) window.rootViewController;
ct.bgView.hidden = YES;
CustomTabbarController.h
#import <UIKit/UIKit.h>
#define kWith [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
@interface CustomTabbarController : UITabBarController
@property (nonatomic,strong) UIView * bgView;
@end
CustomTabbarController.m
#import "CustomTabbarController.h"
@implementation CustomTabbarController
-(instancetype)init{
self = [super init];
if (self) {
[self.tabBar removeFromSuperview];// 移除系统自带tabbar
// self.tabBar.hidden = YES;//隐藏系统自带tabbar 两种方法都能实现让系统自带tabbar消失的效果
self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, kHeight-49, kWith, 49)];
self.bgView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
[self.view addSubview:self.bgView];
}
return self;
}
@end
UITabBar自定义tabbar如何隐藏和显示,超实用UITabBar自定义tabbar如何隐藏和显示,超实用UITabBar自定义tabbar如何隐藏和显示,超实用UITabBar自定义tabbar如何隐藏和显示,超实用UITabBar自定义tabbar如何隐藏和显示,超实用UITabBar自定义tabbar如何隐藏和显示,超实用UITabBar自定义tabbar如何隐藏和显示,超实用UITabBar自定义tabbar如何隐藏和显示,超实用