1. 程式人生 > >設定TabBarItem被選中時的背景

設定TabBarItem被選中時的背景

在TabBarController中設定代理並遵守協議<UITabBarControllerDelegate>

- (void)viewDidLoad {
    [super viewDidLoad];

    self.delegate = self;
    
    // 新增選中的背景
    self.itemSelectedBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab_bg_halo"]];
    self.itemSelectedBg.frame = CGRectMake(0, 0, kScreenW / 4, 49);
    [self.tabBar addSubview:self.itemSelectedBg];
}
實現代理方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSInteger index = tabBarController.selectedIndex;
    self.itemSelectedBg.frame = CGRectMake(index * kScreenW / 4, 0, kScreenW / 4, 49);
}

OK,完事~