iOS Tabbar基本设置

1、修改tabbar高度

  • 重写- (void)viewWillLayoutSubviews方法
//改变tabbar高度
- (void)viewWillLayoutSubviews{
    CGRect tabFrame = self.tabBar.frame;
    tabFrame.size.height = 49;
    tabFrame.origin.y = self.view.frame.size.height - 49;
    self.tabBar.frame = tabFrame;
}

2、修改TabBarItem的文字大小和位置

  • 使用[UITabBarItem appearance]方法,需要注意的是:如果使用 viewcontroller.tarBarItem 设置,如[viewcontroller.tarBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11]} forState:UIControlStateNormal];
    那么为TabBarItem设置的选中颜色会失效
+ (void)initialize {
    
    // 通过appearance统一设置所有UITabBarItem的文字属性
    // 后面带有UI_APPEARANCE_SELECTOR的方法,都可以通过appearance对象来统一设置
    // 正常情况下的属性
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    attrs[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#666666"];
    
    // 选中情况下的属性
    NSMutableDictionary *attrsSelected = [NSMutableDictionary dictionary];
    attrsSelected[NSFontAttributeName] = attrs[NSFontAttributeName]; 
    attrsSelected[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#28D769"];
    UITabBarItem *item = [UITabBarItem appearance];
    
    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:attrsSelected forState:UIControlStateSelected];
    
    [UITabBar appearance].translucent = NO;
    [[UITabBar appearance]setBackgroundImage:WTVImage(@"icon_tabbarImage")];
    
    //设置title的位置
    [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, -3)];
    
}

你可能感兴趣的:(iOS Tabbar基本设置)