UITabBarController 、TabBar背景颜色设置,UITabBarItem的文字样式(颜色和大小)UITabBarItem的位置调整

改变UITabBarController的颜色

    UIView*mView=[[UIView alloc]initWithFrame:CGRectMake(0,0,320,48)];//这是部分tabbar的颜色

    [mView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"ydy1.jpg"]]];

    [self.tabBar insertSubview:mView atIndex:1];

    mView.alpha=0.8;

  效果如图:

 

设置tabBar的背景色

第一种方式:

[[UITabBar appearance] setBackgroundColor:[UIColor redColor]];//测试时不起作用

[[UITabBar appearance] setBarTintColor:[UIColor redColor]];//这样写才能达到效果。

如果想要得到想要的颜色需要再另外添加一句:

[UITabBar appearance].translucent = NO;// 这句表示取消tabBar的透明效果。

第二种方式:在tabBar上添加一个有颜色的View

UIView *view= [[UIView alloc]init];

view.backgroundColor=[UIColor redColor];

view.frame=self.tabBar.bounds;

[[UITabBar appearance] insertSubview:view atIndex:0];

第三种方式:使用背景图片:

[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tabBarBackgroundImage"]];

[UITabBar appearance].translucent = NO;

UITabBarItem的文字样式(颜色和大小)

#define WYColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]

        // 第一种写法

    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];

    textAttrs[NSForegroundColorAttributeName] = WYColor(123, 123, 123);

    textAttrs[NSFontAttributeName]=[UIFont fontWithName:@"Helvetica" size:12.0f];

    

    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];

    selectTextAttrs[NSForegroundColorAttributeName] = WYColor(50, 156, 245);

    selectTextAttrs[NSFontAttributeName]=[UIFont fontWithName:@"Helvetica" size:18.0f];

    

    [[UITabBarItem appearance] setTitleTextAttributes:textAttrs forState:UIControlStateNormal];

    [[UITabBarItem appearance] setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];

      // 第二种写法

 [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName, [UIFont fontWithName:@"Helvetica" size:12.0f],NSFontAttributeName,nil] forState:UIControlStateNormal];

    

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, [UIFont fontWithName:@"Helvetica" size:19.0f],NSFontAttributeName,nil] forState:UIControlStateSelected];

 注意:1、设置字体的时候要选择支持中文的字体,不然的话修改字号是无效的,比如字体设置成“ProximaNova-Semibold”。这种字体本身只支持英语,不支持中文,所以使用该字体并不能调整字体大小。

         2、如果文字太大的话,会和图片靠的很近,这个时候我们可以让UI设计师给我们切图的时候多留一点边白就可以解决。          

UITabBarItem的选中和非选中图片

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];

    nav.tabBarItem.image =[[UIImage imageNamed:@"unselect"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    nav.tabBarItem.selectedImage =[[UIImage imageNamed:@"select"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    nav.tabBarItem.title = @"yoowei";

[self addChildViewController:nav];

调整各个ITEM的位置

        //未选中的图片(上面的数组没有列出)

        UIImage *unselectedImage = [UIImage imageNamed:nor_images[i]];

        unselectedImage = [unselectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

        //选中的图片

        UIImage *selectedImage = [UIImage imageNamed:select_images[i]];

        selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

        UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:arrayName[i] image:unselectedImage selectedImage:selectedImage];

        //设置背景图片的内凹变化,正值向内缩小,负值向外延伸

        UIEdgeInsets insets;

        insets.left = -5;

        insets.right = -5;

        tabBarItem.imageInsets = insets;

         //设置item.title位置偏移

        UIOffset offset;

        offset.horizontal = 30;

        offset.vertical = -10;

        [tabBarItem setTitlePositionAdjustment:offset];

 效果如下:(虽然很难看,但是效果达到了)。

原文地址:https://www.cnblogs.com/richard-youth/p/4988127.html