使用系统的tabbar解决图片的问题.

用sb使用系统的tabbar,弄好后发现图片始终是蓝色的,并非原来的图片,后经上网查资料,问问同事,有了以下的这些代码,做个标记.

AppDelegate中

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

initTabBarSelectedItem()

return true

}

func initTabBarSelectedItem()

{

var rootController = self.window?

.rootViewController as UITabBarController

var tabBar = rootController.tabBar

    var homeItem = tabBar.items?

[0] as UITabBarItem
var selectedImg = UIImage(named: "首页")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
if homeItem.tag == 100
{
homeItem.selectedImage = selectedImg
homeItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.redColor(),NSFontAttributeName:UIFont.systemFontOfSize(14)], forState: UIControlState.Normal)
}
var orderItem = tabBar.items?[1] as UITabBarItem
var orderItemImg = UIImage(named: "订单")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
if orderItem.tag == 101
{
orderItem.selectedImage = orderItemImg
orderItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.redColor()], forState: UIControlState.Selected)
} var releaseItem = tabBar.items? [2] as UITabBarItem
var releaseItemImg = UIImage(named: "公布")? .imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
if releaseItem.tag == 102
{
releaseItem.selectedImage = releaseItemImg
releaseItem.image = releaseItemImg
releaseItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.redColor()], forState: UIControlState.Selected)
}
var myItem = tabBar.items?[3] as UITabBarItem
var myItemImg = UIImage(named: "我的")? .imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
if myItem.tag == 103
{
myItem.selectedImage = myItemImg
myItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.redColor()], forState: UIControlState.Selected)
} }
05-11 17:03