底部TabBar能够说每一个App的标配了,大部分一个Tab就是App的一个模块的功能首页。在Android中,底部TabBar通常用RadioGroup和RadioButton来自定义,就是单选组和单选按钮。而iOS上则提供了UITabBarController。Android上的TabBar切换通常为Fragment,而iOS上的TabBar切换是切换ViewController。数组
Android直观感受就是给你一堆控件,你本身自由发挥,而iOS则是封装好了给你让你直接用,还把建议给你,按照他来作就行了。bash
UITabBarController就是多个ViewController的容器,他们之间的层级是平行的,它会在底部添加一个TabBar的UIView,经过点击TabBar上的按钮tabBarItem来切换对应的ViewController。app
分为4步走:ui
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
//1.建立Tab所属的ViewController
//首页
HomeViewController *homeVC = [[HomeViewController alloc] init];
UINavigationController *homeNav = [[UINavigationController alloc] initWithRootViewController:homeVC];
homeNav.navigationBar.translucent = NO;
//工做
WorkViewController *workVC = [[WorkViewController alloc] init];
UINavigationController *workNav = [[UINavigationController alloc] initWithRootViewController:workVC];
workNav.navigationBar.translucent = NO;
//通知
NoticeViewController *noticeVC = [[NoticeViewController alloc] init];
UINavigationController *noticeNav = [[UINavigationController alloc] initWithRootViewController:noticeVC];
noticeNav.navigationBar.translucent = NO;
//个人
MineViewController *mineVC = [[MineViewController alloc] init];
UINavigationController *mineNav = [[UINavigationController alloc] initWithRootViewController:mineVC];
mineNav.navigationBar.translucent = NO;
//二、建立一个数组,放置多有控制器
NSArray *vcArray = [NSArray arrayWithObjects:homeNav, workNav, noticeNav, mineNav, nil];
//三、建立UITabBarController,将控制器数组设置给UITabBarController
UITabBarController *tabBarVC = [[UITabBarController alloc] init];
//设置多个Tab的ViewController到TabBarViewController
tabBarVC.viewControllers = vcArray;
//四、将UITabBarController设置为Window的RootViewController
self.window.rootViewController = tabBarVC;
//显示Window
[self.window makeKeyAndVisible];
return YES;
}
@end
复制代码
通过上面的设置,3个Tab的ViewController能显示也能切换。可是TabBar上没有控件显示,TabBar的控件经过UITabBarItem来设置,每一个ViewController都有一个self.tabBarItem属性,经过设置一个属性来设置TabBar上的Tab。下面演示的方法都在ViewContoller中使用。spa
//根据标题、非选中图片、选中图片来构建一个Tab
UITabBarItem *tabItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[[UIImage imageNamed:@"home_icon_home_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"home_icon_home_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
//设置Tab
self.tabBarItem = tabItem;
复制代码
UITabBarItem* tabItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
//设置Tab
self.tabBarItem = tabItem;
复制代码
//设置未读数量字符串
tabItem.badgeValue = @"99+";
复制代码
tabBarVC.tabBar.translucent = NO;
复制代码
tabBarVC.tabBar.barTintColor = [UIColor redColor];
复制代码
tabBarVC.tabBar.tintColor = [UIColor redColor];
复制代码
//选中第三个Tab
tabBarVC.selectedIndex = 2;
复制代码
NSUInteger curSelectIndex = tabBarVC.selectedIndex;
NSLog(@"当前选中的Tab角标:%lu", curSelectIndex);
复制代码
UIViewController *curSelectVC = tabBarVC.selectedViewController;
复制代码
//1.遵循协议UITabBarControllerDelegate
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
@end
//2.设置代理
tabBarVC.delegate = self;
/**
* 当选中控制器时回调
*/
- (void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
//选中的ViewController实例
UIViewController *selectVC = tabBarController.selectedViewController;
NSLog(@"选中的index: %zd, 选中的ViewController: %@", tabBarController.selectedIndex, selectVC);
}
复制代码