tabBarView中的tabBarItem设置

在xcode中直接配置, 总是不起作用.

  1. 设置selected Image:

  2. 设置Runtime Attributes:

不知道是不是xcode的bug

只能通过代码实现:

  • 可以在单独的每个UIView中
 self.tabBarItem.image = UIImage(named: "home")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
 self.tabBarItem.selectedImage = UIImage(named: "home--")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)  
  • 也可在UITabBarController中对所有的tabBar初始化:
 class IndexTabBarViewController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()

        // 设置tabBarItem的图标
        let allItems = self.tabBar.items as! Array<UITabBarItem>
        let firstItem = allItems[0] as UITabBarItem
        firstItem.image = UIImage(named: "我的设备")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        firstItem.selectedImage = UIImage(named: "我的设备red")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        // 设置title的颜色
        let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.redColor()]
        firstItem.setTitleTextAttributes(titleDict as [NSObject : AnyObject], forState: .Selected)
    }
}
2015-06-28 15:235