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

2017-04-24  本文已影响3146人  默默_David

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

-(void)insertSubview:(UIView*)viewatIndex:(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:tabbarBgViewatIndex:0];

//tabbar被选中的背景颜色

CGSizeindicatorImageSize=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);

//获得图形上下文

CGContextRefctx=UIGraphicsGetCurrentContext();

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

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

CGRectrect=CGRectMake(0,0,size.width,size.height);

CGContextAddEllipseInRect(ctx, rect);

CGContextClip(ctx);

UIImage*image=UIGraphicsGetImageFromCurrentImageContext();

[imagedrawInRect:rect];

UIGraphicsEndImageContext();

returnimage;

}

//tabBar和navigationBar结合使用

UIViewController *vc= [UIViewControllernew];

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

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

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

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

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

//设置navigationbar的颜色

[nav.navigationBarsetBarTintColor:kAppNavigationBarColor];

//或设置navigationbar的背景图片

[nav.navigationBarsetBackgroundImage:navBarBackgroundImageforBarMetrics:UIBarMetricsDefault];

上一篇下一篇

猜你喜欢

热点阅读