- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self _initViewController]; [self _initTabbbarView]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //初始化子控制器 -(void)_initViewController{ HomeViewController *home = [[HomeViewController alloc]init]; MsssageViewController *message = [[MsssageViewController alloc]init]; ProfireViewController *profire = [[ProfireViewController alloc]init]; SquareViewController *square = [[SquareViewController alloc]init]; MoreViewController *more = [[MoreViewController alloc]init]; NSArray *views = @[home,message,profire,square,more]; NSMutableArray *navViews=[NSMutableArray arrayWithCapacity:5]; for (UIViewController *viewController in views) { BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:viewController]; [navViews addObject:nav]; } self.viewControllers = navViews; } //创建自定义tabbar -(void)_initTabbbarView{ // [UIScreen mainScreen].bounds.size.height //物理高度 _tabbarView =[[UIView alloc]initWithFrame:CGRectMake(0, ScreenH-49, 320, 49)]; [_tabbarView setBackgroundColor:[UIColor grayColor]]; [self.view addSubview:_tabbarView]; NSArray *background = @[@"tabbar_home",@"tabbar_message",@"tabbar_profile",@"tabbar_discover",@"tabbar_more"]; NSArray *background_high = @[@"tabbar_home_highlighted",@"tabbar_message_highlighted",@"tabbar_profile_highlighted",@"tabbar_discover_highlighted",@"tabbar_more_highlighted"]; NSArray *background_selected = @[@"tabbar_home_selected",@"tabbar_message_selected",@"tabbar_profile_selected",@"tabbar_discover_selected",@"tabbar_more_selected"]; for (int i=0;i<background.count;i++) { NSString *backImage = background[i]; NSString *backgImage_high = background_high[i]; NSString *backImageSelected = background_selected[i]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame =CGRectMake((320/5-30)/2+(i*(320/5)), (49-30)/2, 30, 30); [btn setImage:[UIImage imageNamed:backImage] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:backgImage_high] forState:UIControlStateHighlighted]; [btn setImage:[UIImage imageNamed:backImageSelected] forState:UIControlStateSelected]; [btn setTag:i]; [btn addTarget:self action:@selector(selectedTab:) forControlEvents:UIControlEventTouchUpInside]; [_tabbarView addSubview:btn]; } } -(void)selectedTab:(UIButton *)sender{ UIButton *btn =sender; self.selectedIndex=btn.tag; }