UITabBar -- 定制

- (void)viewDidLoad {
    [super viewDidLoad];

    tabBarViewController = [[UITabBarController alloc] init];
    tabBarViewController.view.backgroundColor = [UIColor blackColor];
    tabBarViewController.delegate = self;
    first = [[SceneListVC alloc] init];
    second = [[FavoriteVC alloc] init];
    
    [self addChildVc:first title:@"场景" image:@"场景未选中" selectedImage:@"场景选中"];
    [self addChildVc:second title:@"收藏" image:@"收藏未选中" selectedImage:@"收藏选中"];

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:TAB_BAR_ITEM_TEXT_FONT, NSFontAttributeName, nil] forState:UIControlStateNormal];
    
    //ios 5+6
    [[UITabBar appearance] setTintColor:[UIColor blackColor]];
    //ios 7+
    [[UITabBar appearance] setBarTintColor:[UIColor blackColor]];
    tabBarViewController.tabBar.alpha = 0.8f;

    [self.view addSubview:tabBarViewController.view];
    [self.view bringSubviewToFront:tabBarViewController.view];

    

}





- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)imageName selectedImage:(NSString *) selectedImageName{
    childVc.title = title;
    
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    textAttrs[NSFontAttributeName] = TAB_BAR_ITEM_TEXT_FONT;
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = TAB_BAR_ITEM_TEXT_COLOR;
    selectTextAttrs[NSFontAttributeName] = TAB_BAR_ITEM_TEXT_FONT;
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    
    NavigationVC *nav = [[NavigationVC alloc] initWithRootViewController:childVc];
    [tabBarViewController addChildViewController:nav];
}


你可能感兴趣的:(UITabBar -- 定制)