方法一
用自定義tabBar替換系統(tǒng)tabBar, 然后調(diào)用self.hidesBottomBarWhenPushed = YES
直接隱藏
代碼實(shí)現(xiàn)
自定義tabBar
首先自定義一個(gè)tabBar繼承于UItabBar
在自定義tabBar的.h文件中輸入
@property (nonatomic, weak) UIButton *pButton;
在.m文件開(kāi)始布局自定義tabBar
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
//添加發(fā)布按鈕
UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
[publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"]
forState:UIControlStateNormal];
[publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"]
forState:UIControlStateSelected];
[self addSubview:publishButton];
self.pButton = publishButton;
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
//設(shè)置發(fā)布按鈕的 frame
self.pButton.size = self.publishButton.currentBackgroundImage.size;
self.pButton.center = CGPointMake(self.width * 0.5, self.height * 0.5);
//設(shè)置其他 UITabBarButton 的 frame
CGFloat buttonY = 0;
CGFloat buttonW = self.width / 5;
CGFloat buttonH = self.height;
NSInteger buttonIndex = 0;
for (UIView *button in self.subviews) {
/**
計(jì)算按鈕 X 值, 為了空出中間的距離
從第二個(gè)tabBarButton起, 第三個(gè)按鈕為我們自定義的按鈕, 第四個(gè)第五個(gè)為系統(tǒng)的按鈕
index從0起, 那么當(dāng)buttonIndex > 1
也就是我們看到的第四個(gè)按鈕起
X值就為按鈕寬度 * (按鈕下標(biāo) + 1)
*/
CGFloat buttonX = 0;
if (buttonIndex > 1) {
buttonX = buttonW * (buttonIndex + 1);
} else {
buttonX = buttonW * buttonIndex;
}
//如果按鈕不是tabBarButton, 那么不管它, 繼續(xù)
if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
continue;
}
button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
//增加索引
buttonIndex++;
}
}
自定義tabBarController
在tabBarController中用自定義tabBar替換系統(tǒng)自身tabBar
注意:
self.tabBar = sinaTabBar;
是行不通的, 大家可以試試, 因?yàn)閟elf.tabBar是私有屬性.
但是可以用****KVC****替換
SinaTabBar *sinaTabBar = [[SinaTabBar alloc] init];
[sinaTabBar.pButton addTarget:self
action:@selector(popToView)
forControlEvents:UIControlEventTouchUpInside];
//替換系統(tǒng)tabBar為自定義的
[self setValue:sinaTabBar forKeyPath:@"tabBar"];
完整代碼
自定義tabBarController實(shí)現(xiàn)類(lèi)微博tabBar
#import "SinaTabBarController.h"
#import "FirstController.h"
#import "SecondController.h"
#import "ThirdController.h"
#import "FourthController.h"
#import "SinaTabBar.h"
@interface SinaTabBarController ()
@property (nonatomic, strong) UIView *popView;
@end
@implementation XMGTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//添加子控制器
[self addVC:[[FirstController alloc] init]
title:@"一"
image:@"tabBar_First_icon"
selectedImage:@"tabBar_First_icon_click"];
[self addVC:[[SecondController alloc] init]
title:@"二"
image:@"tabBar_second_icon"
selectedImage:@"tabBar_second_icon_click"];
[self addVC:[[ThirdController alloc] init]
title:@"三"
image:@"tabBar_third_icon"
selectedImage:@"tabBar_third_icon_click"];
[self addVC:[[FourthController alloc] init]
title:@"四"
image:@"tabBar_fourth_icon"
selectedImage:@"tabBar_fourth_icon_click"];
SinaTabBar *sinaTabBar = [[SinaTabBar alloc] init];
//彈出 popView 事件
[sinaTabBar.pButton addTarget:self
action:@selector(popToView)
forControlEvents:UIControlEventTouchUpInside];
//更換 tabBar
[self setValue:sinaTabBar forKeyPath:@"tabBar"];
}
/**
初始化自控制器
@param title tabBarItem名
@param imageName 圖片名
@param selectedImageName 選中圖片名
*/
- (void)addVC:(UIViewController *)vc
title:(NSString *)title
image:(NSString *)imageName
selectedImage:(NSString *)selectedImageName {
vc.tabBarItem.title = title;
vc.tabBarItem.image = [UIImage imageNamed:imageName];
vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self addChildViewController:nav];
}
#pragma mark - 點(diǎn)擊彈出的視圖
- (void)popToView {
//點(diǎn)擊中間的按鈕彈出一個(gè)視圖
self.popView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
self.popView.backgroundColor = [UIColor whiteColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
label.text = @"彈出視圖";
label.textColor = [UIColor blackColor];
[self.popView addSubview:label];
[self.view addSubview:self.popView];
//底部關(guān)閉按鈕
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, kScreenHeight - 49, kScreenWidth, 49);
[button setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
button.backgroundColor = [UIColor lightGrayColor];
[button addTarget:self action:@selector(tapToRemove) forControlEvents:UIControlEventTouchUpInside];
[self.popView addSubview:button];
}
#pragma mark - 點(diǎn)擊彈出視圖上的關(guān)閉按鈕
- (void)tapToRemove {
//移出當(dāng)前遮蓋的視圖
[self.popView removeFromSuperview];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
此方法直接在二級(jí)頁(yè)面控制器中調(diào)用self.hidesBottomBarWhenPushed = YES
即可隱藏自定義的tabBar
方法二
不用新建繼承于tabBar的子控件
隱藏系統(tǒng)tabBar, 用和 tabBar 一樣高度的View添加在 tabBar 處, 這個(gè)方法不可以用self.hidesBottomBarWhenPushed = YES方法實(shí)現(xiàn)跳轉(zhuǎn)隱藏
代碼實(shí)現(xiàn)
自定義tabBarController
在自定義tabBarController聲明方法中添加
@property (nonatomic, strong) UIView *sinaTabBar;
在.m中實(shí)現(xiàn)代碼
#import "SinaTabBarController.h"
#import "FViewController.h"
#import "SViewController.h"
#import "TViewController.h"
#import "FOViewController.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@interface SinaTabBarController ()
//中間彈出視圖
@property (nonatomic, strong) UIView *popView;
@end
@implementation SinaTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setTabBar];
[self setPopView];
FViewController *first = [[FViewController alloc] init];
SViewController *second = [[SViewController alloc] init];
TViewController *third = [[TViewController alloc] init];
FOViewController *fourth = [[FOViewController alloc] init];
UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:first];
UINavigationController *secondNav = [[UINavigationController alloc] initWithRootViewController:second];
UINavigationController *thirdNav = [[UINavigationController alloc] initWithRootViewController:third];
UINavigationController *fourthNav = [[UINavigationController alloc] initWithRootViewController:fourth];
self.viewControllers = @[firstNav, secondNav, thirdNav, fourthNav];
}
#pragma mark - 點(diǎn)擊彈出視圖上的關(guān)閉按鈕
- (void)tapToRemove {
//移出當(dāng)前遮蓋的視圖
[self.popView removeFromSuperview];
}
#pragma mark - 點(diǎn)擊自定義的 tabBar 視圖上的按鈕
- (void)tapButton:(UIButton *)button {
if (button.tag == 2) {
[self.view addSubview:self.popView];
} else if (button.tag >= 3) {
//因?yàn)橛形鍌€(gè)按鈕, 而只有4個(gè) viewController, selectedIndex 會(huì)向前移動(dòng)一個(gè)
[self setSelectedIndex:button.tag - 1];
} else {
[self setSelectedIndex:button.tag];
}
}
#pragma mark - 配置 tabBarView
- (void)setTabBar {
self.tabBar.hidden = YES;
self.sinaTabBar = [[UIView alloc] initWithFrame:self.tabBar.frame];
self.sinaTabBar.backgroundColor = [UIColor colorWithRed:0.134 green:0.283 blue:0.344 alpha:0.195];
self.sinaTabBar.tag = 1000;
[self.view addSubview:self.sinaTabBar];
for (NSInteger i = 0; i < 5; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(i * ScreenWidth / 5, 0, ScreenWidth * 0.2, self.tabBar.frame.size.height);
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"tabBar%ld", i]];
[button setImage:image forState:UIControlStateNormal];
button.tag = i;
[button addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
[self.sinaTabBar addSubview:button];
}
}
#pragma mark - 初始化彈出的視圖
- (void)setPopView {
//點(diǎn)擊中間的按鈕彈出一個(gè)視圖
self.popView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
self.popView.backgroundColor = [UIColor whiteColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(200, 200, 100, 40)];
label.text = @"彈出視圖";
label.textColor = [UIColor blackColor];
[self.popView addSubview:label];
//底部關(guān)閉按鈕
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, ScreenHeight - 49, ScreenWidth, 49);
[button setImage:[UIImage imageNamed:@"5"] forState:UIControlStateNormal];
button.backgroundColor = [UIColor lightGrayColor];
[button addTarget:self action:@selector(tapToRemove) forControlEvents:UIControlEventTouchUpInside];
[self.popView addSubview:button];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
跳轉(zhuǎn)隱藏
在二級(jí)頁(yè)面控制器中調(diào)用兩個(gè)視圖加載方法:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIView *sinaBar = [self.tabBarController.view viewWithTag:1000];
[UIView animateWithDuration:0.25 animations:^{
sinaBar.frame = CGRectMake(0, kScreenHeight, kTabBarWidth, kTabBarHeight);
}];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
UIView *sinaBar = [self.tabBarController.view viewWithTag:1000];
[UIView animateWithDuration:0.25 animations:^{
sinaBar.frame = CGRectMake(0, kScreenHeight- kTabBarHeight, kTabBarWidth, kTabBarHeight);
}];
}
這樣會(huì)有一個(gè)隱藏的動(dòng)畫(huà)效果, 也可以不用, 就和系統(tǒng)的隱藏方法一樣了
PS:
方法一中也可以在viewWill的兩個(gè)方法中用偏移量解決
self.tabBarcontroller.tabBar.frame = (...)
彈出的View上子控件的動(dòng)畫(huà)
這里推薦一個(gè)國(guó)外大牛的三方
facebook/pop
動(dòng)畫(huà)很全面了
自己設(shè)置好初始坐標(biāo)和結(jié)束坐標(biāo), 添加動(dòng)畫(huà)
這里就不再贅述了