自定义tabBar的背景以及item

由于公司项目,需要一个透明的tabbar,结果发现sb 中无法设置。于是在代码中实现
用到几个关键的属性


//设置uiimage

  1. image.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    //设置背景

  2. tabBar.backgroundImage
    //去掉横线

  3. tabBar.shadowImage
    // 获取每个item
    4.var items:[UITabBarItem] = self.tabBar.items as! [UITabBarItem]
    //设置item 的图片,以及选中效果图片
    items[0].image
    items[0].selectedImage


在viewDidLoad()中加入以下代码

var image :UIImage=UIImage(named: "tabbarbg.png")! self.tabBar.backgroundImage=image.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
self.tabBar.shadowImage=image.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
self.tabBar.frame=CGRectMake(0, UIScreen.mainScreen().bounds.height-70,
UIScreen.mainScreen().bounds.width, 84);

 var items:[UITabBarItem] = self.tabBar.items as! [UITabBarItem]
 var image1 :UIImage=UIImage(named: "commen_home_false")!
 items[0].image=image1.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
 var select_image1 :UIImage=UIImage(named: "commen_home")!
 items[0].selectedImage=select_image1.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
 var image2 :UIImage=UIImage(named: "commen_nearby_false")!
 items[1].image=image2.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
 var select_image2 :UIImage=UIImage(named: "commen_nearby")!
 items[1].selectedImage=select_image2.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
 
 var image3 :UIImage=UIImage(named: "commen_calendar_false")!
 items[2].image=image3.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
 var select_image3 :UIImage=UIImage(named: "commen_calendar")!
 items[2].selectedImage=select_image3.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
 var image4 :UIImage=UIImage(named: "commen_calendar")!
 items[3].image=image4.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

实现效果

自定义tabBar的背景以及item_第1张图片
tab.png

你可能感兴趣的:(自定义tabBar的背景以及item)