iOS tabBarItem的选中与非选中时,背景颜色,字体颜色

//设置tabBar的背景颜色,使用的方法

- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;

//设置tabbar的背景

    UIView *tabbarBgView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,self.tabBar.frame.size.width,self.tabBar.frame.size.height)];

    tabbarBgView.backgroundColor =kAppBarColor;

    [self.tabBarinsertSubview:tabbarBgView atIndex:0];


    //tabbar被选中的背景颜色

    CGSize indicatorImageSize =CGSizeMake(self.tabBar.bounds.size.width/5 - 10, self.tabBar.bounds.size.height);

    self.tabBar.selectionIndicatorImage = [selfdrawTabBarItemBackgroundUmageWithSize:indicatorImageSize];

    //字体大小,颜色(未被选中时)

    [[UITabBarItemappearance] setTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:kAppWhiteColor,NSForegroundColorAttributeName, [UIFontfontWithName:@"Helvetica"size:12.0f],NSFontAttributeName,nil]forState:UIControlStateNormal];

     //字体大小,颜色(被选中时)

    [[UITabBarItemappearance] setTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:kAppNavigationBarColor,NSForegroundColorAttributeName, [UIFontfontWithName:@"Helvetica"size:12.0f],NSFontAttributeName,nil]forState:UIControlStateSelected];


//绘制图片

-(UIImage *)drawTabBarItemBackgroundUmageWithSize:(CGSize)size

{

    //开始图形上下文

    UIGraphicsBeginImageContext(size);

    //获得图形上下文

    CGContextRef ctx =UIGraphicsGetCurrentContext();

    

    CGContextSetRGBFillColor(ctx,1, 1,1, 1);

    CGContextFillRect(ctx,CGRectMake(5,0, size.width, size.height));

    

    

    CGRect rect =CGRectMake(0,0, size.width, size.height);

    CGContextAddEllipseInRect(ctx, rect);

    

    CGContextClip(ctx);

    

    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();

    

    [image drawInRect:rect];

    

    UIGraphicsEndImageContext();

    

    return image;

}


//tabBar和navigationBar结合使用

    UIViewController *vc = [UIViewController new];

    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];

    vc.tabBarItem.title = @"首页";

    //设置tabbar被选中与未被选中的图片

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

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

    //设置navigationbar的颜色

    [nav.navigationBar setBarTintColor:kAppNavigationBarColor];

//或设置navigationbar的背景图片

[nav.navigationBar setBackgroundImage:navBarBackgroundImage forBarMetrics:UIBarMetricsDefault];


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值