設定TabBarItem被選中時的背景
阿新 • • 發佈:2019-01-02
在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,完事~