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];
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章