項(xiàng)目中有一個(gè)需求,在滑動(dòng)列表中上拉隱藏導(dǎo)航欄與tabBar,下拉顯示導(dǎo)航欄與tabBar的功能 ,開發(fā)中也踩了一些小坑,所以空閑時(shí)間我把這個(gè)功能點(diǎn)抽取出來(lái)供有需要的開發(fā)者參考,有不足之處請(qǐng)指出,謝謝。
參考文章:鎮(zhèn)屌要逆襲的文章? http://www.itdecent.cn/p/40fa40c65124
先看一下效果圖:


做法不難,導(dǎo)航欄只要設(shè)置setNavigationBarHidden顯示與隱藏就能達(dá)到想要的效果,而tabBar則需要改變frame來(lái)達(dá)到想要的效果。
直接上代碼,看demo:
先在AppDelegate中創(chuàng)建UITabBarController與UINavigationController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = self.tabBar;
[self.window makeKeyAndVisible];
return YES;
}
/*
* 創(chuàng)建控制器
*/
- (void)setupVC{
MYNavTabBarDemoVC *vc = [MYNavTabBarDemoVC new];
UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首頁(yè)" image:[UIImage imageNamed:@"home"] tag:0];
[tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];
[tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateSelected];
vc.tabBarItem = tabBarItem;
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
self.tabBar = [UITabBarController new];
self.tabBar.viewControllers = @[nav];
}
主要代碼在滑動(dòng)的控制器中,下面看幾個(gè)重要的方法設(shè)置。
1.要設(shè)置兩個(gè)重要的屬性 extendedLayoutIncludesOpaqueBars 與 edgesForExtendedLayout
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.extendedLayoutIncludesOpaqueBars = NO;
self.edgesForExtendedLayout = UIRectEdgeNone;
}
2.在scrollViewWillEndDragging代理中實(shí)現(xiàn)顯示與隱藏導(dǎo)航欄與tabBar
#pragma mark 滑動(dòng)隱藏導(dǎo)航欄與tabBar
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
if(velocity.y>0){
[self.navigationController setNavigationBarHidden:YES animated:YES];
[self setTabBarHidden:YES];
}else{
[self.navigationController setNavigationBarHidden:NO animated:YES];
[self setTabBarHidden:NO];
}
}
/*
* 隱藏顯示tabbar
*/
- (void)setTabBarHidden:(BOOL)hidden
{
UIView *tab = self.tabBarController.view;
tab.backgroundColor = [UIColor clearColor];
CGRect? tabRect=self.tabBarController.tabBar.frame;
if ([tab.subviews count] < 2) {
return;
}
UIView *view;
if ([[tab.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]]) {
view = [tab.subviews objectAtIndex:1];
} else {
view = [tab.subviews objectAtIndex:0];
}
view.backgroundColor = [UIColor clearColor];
if (hidden) {
view.frame = tab.bounds;
tabRect.origin.y=[[UIScreen mainScreen] bounds].size.height+self.tabBarController.tabBar.frame.size.height;
self.myTableView.frame = [UIScreen mainScreen].bounds;
[tab bringSubviewToFront:view];
[view sendSubviewToBack:self.tabBarController.tabBar];
[UITabBar appearance].translucent = YES;
} else {
view.frame = CGRectMake(tab.bounds.origin.x, tab.bounds.origin.y, tab.bounds.size.width, tab.bounds.size.height);
tabRect.origin.y=[[UIScreen mainScreen] bounds].size.height-self.tabBarController.tabBar.frame.size.height;
self.myTableView.frame = view.frame;
[tab bringSubviewToFront:self.tabBarController.tabBar];
[self.tabBarController.tabBar sendSubviewToBack:view];
[UITabBar appearance].translucent = NO;
}
[UIView animateWithDuration:0.5f animations:^{
self.tabBarController.tabBar.frame=tabRect;
}completion:^(BOOL finished) {
}];
}
到此,項(xiàng)目想要的上下拉隱藏與顯示導(dǎo)航欄與tabBar的功能就實(shí)現(xiàn)了。以上只是我個(gè)人的實(shí)現(xiàn)方法,如果你有更好做法,歡迎騷擾交流。覺(jué)得有幫助,請(qǐng)start一個(gè)吧。如要轉(zhuǎn)載,請(qǐng)求標(biāo)明出處,謝謝。