UITabBar自定义方式二(容器类控制器)

来源:互联网 发布:安徽八度网络 编辑:程序博客网 时间:2024/06/06 14:23
1.新建类继承于UIView,并在.h公布一个属性,这个属性是选择的按钮下标@property (nonatomic, assign) NSInteger selectedIndex;2.实现代码#pragma mark - 自定义标签栏//1.自定义标签栏- (void)customTabBar{       //    计算每个按钮的宽度    CGFloat width = ScreenW / 5.0;          //    1.创建标签栏对象    _tabBarView = [[UIImageView alloc] initWithFrame:CGRectMake(0, ScreenH - 49, ScreenW, 49)];    [self.view addSubview:_tabBarView];    _tabBarView.userInteractionEnabled = YES;       //    2.设置标签栏的背景图片    _tabBarView.image = [UIImage imageNamed:@"mask_navbar.png"];       //    3.创建选中图片    _selectedImage = [[UIImageView alloc] initWithFrame:CGRectMake((width - 64) / 2.0, 0, 64, 49)];    [_tabBarView addSubview:_selectedImage];    _selectedImage.image = [UIImage imageNamed:@"home_bottom_tab_arrow.png"];               //    4.创建按钮    NSArray *btnImage = @[@"home_tab_icon_1",                          @"home_tab_icon_2",                          @"home_tab_icon_3",                          @"home_tab_icon_4",                          @"home_tab_icon_5",                          @"home_tab_icon_6",];       for (int i=0; i<6; i++) {               UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];               //设置默认状态的图片        [button setImage:[UIImage imageNamed:btnImage[i]] forState:UIControlStateNormal];               //设置tag        button.tag = i + 100;               //添加点击事件        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];               //设置frame        CGFloat buttonX = width * i;        CGFloat buttonY = 0;        CGFloat buttonW = width;        CGFloat buttonH = 49;        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);        [_tabBarView insertSubview:button belowSubview:_selectedImage];           }         }#pragma mark - 创建 视图控制器//2.创建控制器- (void)_initCtrls {       //1.创建视图控制器    RootNavigationViewCtrl *homeCtrl = [[UIStoryboard storyboardWithName:@"HomeViewController" bundle:nil] instantiateInitialViewController];       RootNavigationViewCtrl *messageCtrl = [[UIStoryboard storyboardWithName:@"MessageViewController" bundle:nil] instantiateInitialViewController];       RootNavigationViewCtrl *centerCtrl = [[UIStoryboard storyboardWithName:@"CenterViewController" bundle:nil] instantiateInitialViewController];       RootNavigationViewCtrl *discoverCtrl = [[UIStoryboard storyboardWithName:@"DiscoverViewController" bundle:nil] instantiateInitialViewController];       RootNavigationViewCtrl *moreCtrl = [[UIStoryboard storyboardWithName:@"MoreViewController" bundle:nil] instantiateInitialViewController];       NSArray *navCtrls = @[homeCtrl,messageCtrl,centerCtrl,discoverCtrl,moreCtrl];          //2.取出视图控制器,给视图控制器添加子控制器    for (UIViewController *nav in navCtrls) {        [self addChildViewController:nav];    }          //3.取出第一个视图控制器并显示    UIViewController *homeViewCtrl = self.childViewControllers[0];    [self.view addSubview:homeViewCtrl.view];       //4.将视图控制器view放到自定义的tabBar工具栏后面    [self.view insertSubview:homeViewCtrl.view belowSubview:_tabBarView];   }#pragma mark - tabBar按钮触发时间- (void)buttonAction:(UIButton *)button { //    1.动画移动选中图片的位置    [UIView animateWithDuration:.35 animations:^{         _selectedImage.center = button.center;    }];      self.selectedIndex = button.tag - 100;}//实现视图的切换- (void)setSelectedIndex:(NSInteger)selectedIndex{    _selectedIndex = selectedIndex;   //    1.取得老得视图控制器    UIViewController *currentVC = self.childViewControllers[_selectedIndex];   //    2.移除    [currentVC.view removeFromSuperview];   //    3.取得新的视图控制器    UIViewController *newVC = self.childViewControllers[selectedIndex];   //    4.添加    [self.view insertSubview:newVC.view belowSubview:_tabBarView];   }

0 0
原创粉丝点击