iOS上下拉隱藏與顯示導(dǎo)航欄與tabBar

項(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)明出處,謝謝。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容