代码tabBar

效果

代码tabBar_第1张图片
6095F351-642D-4423-8A39-AD77311B091B.png

1.建一个类TabbarViewController继承UITabBarController

  1. 封装添加子控制器的方法
/**
 *  封装添加子控制器的方法
 *
 *  @param childVc       控制器
 *  @param title         标题
 *  @param image         正常状态下的的图标
 *  @param selectedImage 选中状态下的图标
 */
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    //设置标题
    childVc.tabBarItem.title = title;
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    
    //需要设置照片的模式,用照片原图,默认是蓝色的
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //创建修改字体颜色的字典,同时可以设置字体的内边距;
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:123/255 green:123/255 blue:123/255 alpha:1];
    NSMutableDictionary *selectedTextAttrs = [NSMutableDictionary dictionary];
    selectedTextAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];
    childVc.view.backgroundColor = [UIColor yellowColor];
    
     UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:childVc];
    //不要忘记添加到父控制器上
    [self addChildViewController:nav];
}

3.添加控制器

- (void)viewDidLoad {
    [super viewDidLoad];
        HomeViewController *home = [[HomeViewController alloc] init];
        [self addChildVc:home title:@"首页" image:@"index_shop"       selectedImage:@"index_shop_seleted"];
    
         MessageCenterViewController *message = [[MessageCenterViewController alloc] init];
         [self addChildVc:message title:@"消息" image:@"index_message" selectedImage:@"index_message_seleted"];
    
         DiscoverViewController *discover = [[DiscoverViewController alloc] init];
         [self addChildVc:discover title:@"发现" image:@"index_quanzi" selectedImage:@"index_quanzi_seleted"];
    
         ProfileViewController *profile = [[ProfileViewController alloc] init];
         [self addChildVc:profile title:@"我" image:@"index_my" selectedImage:@"index_my_seleted"];
}

你可能感兴趣的:(代码tabBar)