iOS定制viewiOS 开发 iOS开发

iOS利用`quartz2D`绘出tabBar的背景图片

2016-06-11  本文已影响247人  船长_
Snip20160611_31.png

1.场景需求:

2.分析

下面利用quartz2D技术绘制背景图片

3.核心代码

-(void)configTabBars{
  CGSize tabSize = CGSizeMake(CGRectGetWidth(self.tabBarController.view.frame), 49);
  UIGraphicsBeginImageContext(tabSize);
  CGContextRef context = UIGraphicsGetCurrentContext();

  // draw whole image background color
  CGContextSetFillColorWithColor(context,[FTFColor colorWithHexString:@"#383838" withAlpha:1.0].CGColor);
  CGContextFillRect(context, CGRectMake(0, 0, tabSize.width, tabSize.height));
  
  // draw special middle item color
  CGContextSetFillColorWithColor(context, [FTFColor colorWithHexString:@"0x2b2b2b"].CGColor);
  CGFloat width=tabSize.width/5;
  CGContextFillRect(context, CGRectMake(width*2, 0, width, tabSize.height));
  
  UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  
  // setting tabBar backgroundImage
  self.tabBarController.tabBar.backgroundImage = img;
  // setting tabBar image color
  self.tabBarController.tabBar.tintColor = [FTFColor mainGolden];
  
  // setting tabBar title color
  for (UITabBarItem *item in self.tabBarController.tabBar.items) {
   // item.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName:[FTFColor mainGolden]} forState:UIControlStateSelected];
  }
}
上一篇 下一篇

猜你喜欢

热点阅读