iOS使用UITabbarController跳轉(push)介面,如何自動隱藏底部tabbar?
阿新 • • 發佈:2019-02-14
一、首先簡單地講一下UITabbarController的使用方法,直接上程式碼:
二、如何自動隱藏底部的tabbar,在跳轉的時候//初始化tabbarcontroller - (void)setTabbarController{ NSArray *array = @[contactsNav, businessNav, infoNav, myCoffeeNav]; _tabBarController = [[UITabBarController alloc] init]; [_tabBarController setViewControllers:array]; //隱藏黑色線條 [_tabBarController.tabBar setClipsToBounds:YES]; //設定tabbar的背景顏色 [[UITabBar appearance] setBackgroundColor:kTabbarBgColor]; [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]]; [_tabBarController setSelectedViewController:array[0]]; //自定義加上一條線 UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 0.5)]; lineLabel.backgroundColor = HEX_COLOR(@"E5E5E5"); [_tabBarController.tabBar addSubview:lineLabel]; //設定item [self customizeTabBarForController:_tabBarController]; } //設定tabbarItem - (void)customizeTabBarForController:(UITabBarController *)tabBarController { NSInteger index = 0; NSArray *tabbarImages = @[@"btn_foot_friends", @"btn_foot_bussines", @"btn_foot_message", @"btn_foot_coffee"]; for (UITabBarItem *item in [[tabBarController tabBar] items]) { UIImage *selectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_hover",tabbarImages[index]]]; UIImage *unselectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@",tabbarImages[index]]]; [item setSelectedImage:[selectedimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]]; [item setImage:unselectedimage]; CRNavigationController *nav = tabBarController.viewControllers[index]; NSString *titleStr = [nav.viewControllers[0] title]; item.title = titleStr; [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:HEX_COLOR(@"818C9E"),NSForegroundColorAttributeName,[UIFont systemFontOfSize:10],NSFontAttributeName, nil] forState:UIControlStateNormal]; [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:HEX_COLOR(@"E24943"),NSForegroundColorAttributeName,[UIFont systemFontOfSize:10],NSFontAttributeName, nil] forState:UIControlStateSelected]; item.titlePositionAdjustment = UIOffsetMake(0, -3); index++; } }
第一步、在UIViewController的基類中新增如下程式碼,我使用的基類名是BaseViewController:
@implementation BaseViewController
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.hidesBottomBarWhenPushed = NO;
}
@end
第二部、在CRNavigationController(繼承於UINavigationController)中重構push方法,程式碼如下:
@implementation CRNavigationController - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{ self.topViewController.hidesBottomBarWhenPushed = YES; viewController.hidesBottomBarWhenPushed = YES; [super pushViewController:viewController animated:animated]; } @end
結尾:至此頁面間的跳轉可以自動隱藏顯示底部的Tabbar