- (void)viewDidLoad { [super viewDidLoad]; // 使用系统自带的tabBar注意这两个函数的先后顺序 [self createViewControllers]; [self createMyTabBarItem]; }
- (void)createMyTabBarItem { // 移除系统自带的tabBarItem for (UIView * view in self.tabBar.subviews) { [view removeFromSuperview]; } // // PUSH到下一个界面的时候,BottomBar自动隐藏 // self.hidesBottomBarWhenPushed = YES; NSArray * imgs = @[@"movie",@"cinema",@"me"]; NSArray * preImgs = @[@"movie_press",@"cinema_press",@"me_press"]; // 各个item之间的间距 CGFloat space = (__kScreenWidth-33*3)/4; for (int i=0; i<imgs.count; i++) { UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(space+(33+space)*i, 10, 33, 33); UIImage * image = [UIImage imageNamed:imgs[i]]; image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; [btn setBackgroundImage:image forState:UIControlStateNormal]; UIImage * pressImg = [UIImage imageNamed:preImgs[i]]; pressImg = [pressImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; [btn setBackgroundImage:pressImg forState:UIControlStateSelected]; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; btn.tag = 100+i; [self.tabBar addSubview:btn]; // 默认启动显示是第一个界面 if (i==0) { btn.selected = YES; } } }
- (void)btnClick:(UIButton *)sender { // 设置视图切换 NSInteger index = sender.tag-100; self.selectedIndex = index; for (UIView * view in self.tabBar.subviews) { // 要判断view的类型 if ([view isKindOfClass:[UIButton class]]) { UIButton * btn = (UIButton *)view; if (btn.tag == sender.tag) { btn.selected = YES; } else { btn.selected = NO; } } } /* self.tabBar.subviews.count = 5; self.tabBar.subviews: ( "<_UITabBarBackgroundView: 0x7fc513ff48c0; frame = (0 0; 375 49); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fc513ff4c40>>", "<UIButton: 0x7fc513e238e0; frame = (56.25 0; 50 49); opaque = NO; tag = 100; layer = <CALayer: 0x7fc513e23d40>>", "<UIButton: 0x7fc513fda4b0; frame = (162.5 0; 50 49); opaque = NO; tag = 101; layer = <CALayer: 0x7fc513fda6d0>>", "<UIButton: 0x7fc513fe4520; frame = (268.75 0; 50 49); opaque = NO; tag = 102; layer = <CALayer: 0x7fc513fe1920>>", "<UIImageView: 0x7fc513ff5ab0; frame = (0 -0.5; 375 0.5); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fc513ff5720>>" ) */ }
- (void)createViewControllers { NSMutableArray * viewControllers = [[NSMutableArray alloc] init]; // NSArray * classNames = @[@"MovieViewController",@"CinemaViewController",@"SettingViewController"]; // for (NSInteger i=0; i<classNames.count; i++) { // //将字符串表示的类名转换为类(对象) // Class class = NSClassFromString(classNames[i]); // BaseViewController * bVC = [[class alloc] init]; // // UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:bVC]; // [viewControllers addObject:nav]; // // } MovieViewController * MVC = [[MovieViewController alloc] init]; [MVC becomeObserve]; UINavigationController * Mnav = [[UINavigationController alloc] initWithRootViewController:MVC]; [viewControllers addObject:Mnav]; CinemaViewController * CVC = [[CinemaViewController alloc] init]; [CVC becomeObserve]; UINavigationController * Nnav = [[UINavigationController alloc] initWithRootViewController:CVC]; [viewControllers addObject:Nnav]; SettingViewController * SVC = [[SettingViewController alloc] init]; UINavigationController * Snav = [[UINavigationController alloc] initWithRootViewController:SVC]; [viewControllers addObject:Snav]; self.viewControllers = viewControllers; }