UITabBar使用详解

UITabBarController使用得非常广泛,说说它的核心,UITabBar的使用。

建一个工程,创建4个UIViewController用来作切换的视图控制器,为了方便,就在AppDelegate.m文件里面写代码,如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    FirstViewController *fvc = [[FirstViewController alloc]init];
    UINavigationController *fNav = [[UINavigationController alloc]initWithRootViewController:fvc];
    
    SecondViewController *svc = [[SecondViewController alloc]init];
    UINavigationController *sNav = [[UINavigationController alloc]initWithRootViewController:svc];
    
    ThirdViewController *tvc = [[ThirdViewController alloc]init];
    UINavigationController *tNav = [[UINavigationController alloc]initWithRootViewController:tvc];
    
    FourthViewController *fourVc = [[FourthViewController alloc]init];
    UINavigationController *fourNav = [[UINavigationController alloc]initWithRootViewController:fourVc];
    
    UITabBarController *tab = [[UITabBarController alloc]init];
    tab.viewControllers = @[fNav,sNav,tNav,fourNav];
    self.window.rootViewController = tab;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

FirstViewController、SecondViewController、ThirdViewController、FourthViewController是UITabBarController的4个标签控制器,为了区别,设置self.view.backgroundColor为不同颜色,创建UITabBarController对象tab并设置好viewControllers属性,设置tab为window的rootViewController。

UITabBarController *tab = [[UITabBarController alloc]init];
    tab.viewControllers = @[fNav,sNav,tNav,fourNav];
    self.window.rootViewController = tab;

运行看看:

UITabBar使用详解_第1张图片


这时没有设置标签的icon和title,所以tabBar上面什么都没有,但是还是可以点击切换。改变一下TabBar的背景色,设置为黑色:

tab.tabBar.barTintColor = [UIColor blackColor];

效果如下:

UITabBar使用详解_第2张图片


下面为添加4个icon和文字,icon和文字是设置在UITabBarItem对象上的,先看看系统自带的图标效果,系统图标和文字是统一的,不用再单独添加文字,修改代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    FirstViewController *fvc = [[FirstViewController alloc]init];
    UINavigationController *fNav = [[UINavigationController alloc]initWithRootViewController:fvc];
    //设置系统tabBarItem1
    fvc.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0];
    
    SecondViewController *svc = [[SecondViewController alloc]init];
    UINavigationController *sNav = [[UINavigationController alloc]initWithRootViewController:svc];
    //设置系统tabBarItem2
    svc.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:1];
    
    ThirdViewController *tvc = [[ThirdViewController alloc]init];
    UINavigationController *tNav = [[UINavigationController alloc]initWithRootViewController:tvc];
    //设置系统tabBarItem3
    tvc.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:2];
    
    FourthViewController *fourVc = [[FourthViewController alloc]init];
    UINavigationController *fourNav = [[UINavigationController alloc]initWithRootViewController:fourVc];
    //设置系统tabBarItem4
    fourVc.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:3];
    
    UITabBarController *tab = [[UITabBarController alloc]init];
    tab.viewControllers = @[fNav,sNav,tNav,fourNav];
    tab.tabBar.barTintColor = [UIColor blackColor];
    self.window.rootViewController = tab;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
效果如下:

UITabBar使用详解_第3张图片

系统默认的选中颜色是蓝色,设置tintColor属性,改变选中标签的颜色:

//设置icon的颜色
    tab.tabBar.tintColor = [UIColor redColor];
效果如下:

UITabBar使用详解_第4张图片

使用自定义图片来设置tabBar,使用initWithTitle:image:tag:方法,苹果官方文档说明图片的大小为30*30,Retina屏为60*60(stackoverflow上有大神指出,UITabBarItem图标的最佳尺寸是48*32

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    FirstViewController *fvc = [[FirstViewController alloc]init];
    UINavigationController *fNav = [[UINavigationController alloc]initWithRootViewController:fvc];
    //设置自定义图片 tabBarItem1
    fvc.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"home"] tag:0];
    
    SecondViewController *svc = [[SecondViewController alloc]init];
    UINavigationController *sNav = [[UINavigationController alloc]initWithRootViewController:svc];
    //设置自定义图片 tabBarItem2
    svc.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"消息" image:[UIImage imageNamed:@"message"] tag:1];
    
    ThirdViewController *tvc = [[ThirdViewController alloc]init];
    UINavigationController *tNav = [[UINavigationController alloc]initWithRootViewController:tvc];
    //设置自定义图片 tabBarItem3
    tvc.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"购物车" image:[UIImage imageNamed:@"car"] tag:2];
    
    FourthViewController *fourVc = [[FourthViewController alloc]init];
    UINavigationController *fourNav = [[UINavigationController alloc]initWithRootViewController:fourVc];
    //设置自定义图片 tabBarItem4
    fourVc.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"个人中心" image:[UIImage imageNamed:@"customer"] tag:3];
    
    UITabBarController *tab = [[UITabBarController alloc]init];
    tab.viewControllers = @[fNav,sNav,tNav,fourNav];
    tab.tabBar.barTintColor = [UIColor blackColor];
    //设置icon的颜色
    tab.tabBar.tintColor = [UIColor redColor];
    self.window.rootViewController = tab;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

效果如下:

UITabBar使用详解_第5张图片


UITabBarItem有一个selectedImage属性,很明显这个是选中时显示的icon,这里需要说明一下,这默认情况下,选中时的icon只会改变颜色(由tintColor设置),如果需要改变选中时的图片,就要设置selectedImage属性,可以单独设置也可以用初始化方法设置

你可能感兴趣的:(iOS那些事)