美文网首页
界面搭建 移除系统自带tabbar上的按钮

界面搭建 移除系统自带tabbar上的按钮

作者: Mr丶炎 | 来源:发表于2016-07-15 12:23 被阅读73次

    修改导航栏上的背景和字体颜色
    移除系统tabbar上的按钮
    按钮的选中与取消

    界面搭建一般都是三级控制器,标签控制器-->导航控制器-->视图控制器

    这里,因为每个标签项管理的视图有的是一样的,所以我们可以用继承的办法

    以阿里星球项目为例, 其他控制器都继承BaseViewController,一些相同的内容都写在这里

    @interface DynamicViewController : BaseViewController

    修改导航栏上的背景

    //1.设置导航栏的背景图片
        [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg"] forBarMetrics:UIBarMetricsDefault];
        
        //2.设置导航栏标题的字体颜色
        NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor whiteColor],
                              NSFontAttributeName:[UIFont systemFontOfSize:16]};
        
        [self.navigationBar setTitleTextAttributes:dic];
    
    

    移除系统tabbar上的按钮

    /**
     *  自定义TabbarController
     
     1.设置tabbarController 的viewControllers
     2.移除系统的按钮和tabbar
     3.创建 自定义的tabbar
     
     */
    @implementation AliTabbarController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        //1.设置tabbarController 的viewControllers
        [self creatViewControllers];
        
        //2.移除系统自带的tabbar的子视图
        [self removeSystemTabbarSubviews];
        
        //3.自定义tabbar 上控件
        [self creatSubViews];
        
        
        
    }
    
    #pragma mark - 1.设置viewController
    -(void)creatViewControllers {
        
        //1.创建视图控制器
        DynamicViewController *dynamic = [[DynamicViewController alloc] init];
        DIscoverViewController *discover = [[DIscoverViewController alloc] init];
        MessageViewController *message = [[MessageViewController alloc] init];
        MineViewController *mine = [[MineViewController alloc] init];
        
        //2.给每一个视图控制器添加导航控制器
        NSArray *controllers = @[dynamic,discover,message,mine];
        
        NSMutableArray *navis = [[NSMutableArray alloc] initWithCapacity:4];
        
        for (UIViewController *viewController in controllers) {
            
            //创建导航控制器
            BaseNavigationController *navigation = [[BaseNavigationController alloc] initWithRootViewController:viewController];
            
            [navis addObject:navigation];
        }
        
        self.viewControllers = navis;
    }
    
    #pragma mark - 2.移除系统自带的tabbar的子视图
    -(void)removeSystemTabbarSubviews {
        
        for (UIView *view in self.tabBar.subviews) {
            NSLog(@"%@",view);
            //判断子视图 是否属于这类 UITabBarButton
            
            //1.[类名 class] 得出这个类
            //2.NSClassFromString(NSString) 使用字符串转换得出一个类
            
            if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                
                [view removeFromSuperview];
            }
        }
    }
    
    #pragma mark - 3.自定义tabbar子视图
    -(void)creatSubViews {
        //1.创建tabbar背景图片视图
    
        UIImageView *bjImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kTabbar)];
        
        
        bjImageView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabBarViewBg"]];
        
        [self.tabBar addSubview:bjImageView];
        
        
        
        
        CGFloat itemWidth = kScreenW / 4.0;
        
        
        NSArray *imageNames = @[@"trends",@"find",@"message",@"my"];
        
        
        //2.创建四个按钮
        for (int  i = 0; i <self.viewControllers.count; i++) {
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            
            //设置按钮frame
            button.frame = CGRectMake(i * itemWidth, 0, itemWidth, kTabbar);
            
            //设置图片
            NSString *imageNormal = [NSString stringWithFormat:@"trends_%@_normal",imageNames[i]];
            NSString *imageSelected = [NSString stringWithFormat:@"trends_%@_click",imageNames[i]];
            
            [button setImage:[UIImage imageNamed:imageNormal] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:imageSelected] forState:UIControlStateSelected];
            
            
            button.tag = 1000 + i;
            
            //设置第一个按钮为选中状态
            if (button.tag == 1000) {
                button.selected = YES;
            }
            
            [button addTarget:self
                       action:@selector(clickBtn:)
             forControlEvents:UIControlEventTouchUpInside];
            
            
            [self.tabBar addSubview:button];
            
        }
    
    }
    
    -(void)clickBtn:(UIButton *)sender {
        
        
        NSInteger index = sender.tag - 1000;
        
        //1.切换视图控制器
        self.selectedIndex = index;
        
        //2.修改按钮的旋钮的选中状态
        for (int i = 0; i < self.viewControllers.count; i++) {
            
            UIButton *btn = (UIButton *)[self.tabBar viewWithTag:1000 + i];
            
            btn.selected = NO;
        }
        
        sender.selected = !sender.selected;
        
        
    }
    
    

    相关文章

      网友评论

          本文标题:界面搭建 移除系统自带tabbar上的按钮

          本文链接:https://www.haomeiwen.com/subject/znjyjttx.html