tabbar使用

1、图标尺寸: @2x  38*38   @3x    57*57

2、创建tabbarItem:

 1)继承UITabbarController直接设置:

        ZCMainViewController*mainVC = [[ZCMainViewController alloc]init];

ZCMessageViewController*messageVC = [[ZCMessageViewController alloc]init];

ZCMeViewController*meVC = [[ZCMeViewController alloc]init];

UINavigationController*mainNav = [[UINavigationController alloc]initWithRootViewController:mainVC];

UINavigationController*messageNav = [[UINavigationController alloc]initWithRootViewController:messageVC];

UINavigationController*meNav = [[UINavigationController alloc]initWithRootViewController:meVC];

mainNav.title=@"首页";

mainNav.tabBarItem.image= [[UIImageimageNamed:@"home1"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

mainNav.tabBarItem.selectedImage= [[UIImageimageNamed:@"home2"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

messageNav.title=@"消息";

messageNav.tabBarItem.image= [[UIImageimageNamed:@"消息1"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

messageNav.tabBarItem.selectedImage= [[UIImageimageNamed:@"消息2"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

meNav.title=@"我的";

meNav.tabBarItem.image= [[UIImageimageNamed:@"home1"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

meNav.tabBarItem.selectedImage= [[UIImageimageNamed:@"home2"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

self.viewControllers= @[mainNav,messageNav,meNav];

 2)创建:

UIOffsettitlePositionAdjustment=UIOffsetMake(0, 0);

UIEdgeInsetsimageInsets=UIEdgeInsetsMake(0, 0, 0, 0);

ZCMainViewController*mainVC = [[ZCMainViewControlleralloc]init];

ZCMessageViewController*messageVC = [[ZCMessageViewControlleralloc]init];

ZCMeViewController*meVC = [[ZCMeViewControlleralloc]init];

UINavigationController*mainNav = [[UINavigationControlleralloc]initWithRootViewController:mainVC];

mainNav.tabBarItem= [[UITabBarItemalloc]initWithTitle:@"首页"

image:[[UIImageimageNamed:@"home1"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]

selectedImage:[[UIImageimageNamed:@"home2"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

[mainNav.tabBarItemsetTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:[UIColorgrayColor],NSForegroundColorAttributeName,nil]

forState:UIControlStateNormal];

[mainNav.tabBarItemsetTitleTextAttributes:@{NSForegroundColorAttributeName:G_RGB(0x38, 0xad, 0xff)}forState:UIControlStateSelected];

[mainNav.tabBarItemsetTitlePositionAdjustment:titlePositionAdjustment];

mainNav.tabBarItem.imageInsets= imageInsets;

//消息

UINavigationController*messageNav = [[UINavigationControlleralloc]initWithRootViewController:messageVC];

messageNav.tabBarItem= [[UITabBarItemalloc]initWithTitle:@"消息"

image:[[UIImageimageNamed:@"消息1"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]

selectedImage:[[UIImageimageNamed:@"消息2"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

[messageNav.tabBarItemsetTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:[UIColorgrayColor],NSForegroundColorAttributeName,nil]

forState:UIControlStateNormal];

[messageNav.tabBarItemsetTitleTextAttributes:@{NSForegroundColorAttributeName:G_RGB(0x38, 0xad, 0xff)}forState:UIControlStateSelected];

[messageNav.tabBarItemsetTitlePositionAdjustment:titlePositionAdjustment];

messageNav.tabBarItem.imageInsets= imageInsets;

//我的

UINavigationController*meNav = [[UINavigationControlleralloc]initWithRootViewController:meVC];

meNav.tabBarItem= [[UITabBarItemalloc]initWithTitle:@"我的"

image:[[UIImageimageNamed:@"消息1"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]

selectedImage:[[UIImageimageNamed:@"消息2"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

[meNav.tabBarItemsetTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:[UIColorgrayColor],NSForegroundColorAttributeName,nil]

forState:UIControlStateNormal];

[meNav.tabBarItemsetTitleTextAttributes:@{NSForegroundColorAttributeName:G_RGB(0x38, 0xad, 0xff)}

forState:UIControlStateSelected];

[meNav.tabBarItemsetTitlePositionAdjustment:titlePositionAdjustment];

meNav.tabBarItem.imageInsets= imageInsets;


3、手动在控制器中设置:

self.title=@"我的";

self.navigationController.tabBarItem.image= [UIImageimageNamed:@"home1"];

self.navigationController.tabBarItem.selectedImage= [UIImageimageNamed:@"home2"];


4、设置tabbar的选中和未选中的字体颜色

[[UITabBarItemappearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColorlightGrayColor] }forState:UIControlStateNormal];

[[UITabBarItemappearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColorblackColor] }forState:UIControlStateSelected];

推荐阅读更多精彩内容