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];


你可能感兴趣的:(iOS开发)