storyboard创建的选项卡,图片颜色固定,字体颜色固定。可以说完全没法用,查阅了好久的api和资料才找到修改的办法。
将UITabBarItem连线到FirstViewController
代码如下:
// // FirstViewController.swift // demo // // Created by sunger on 15/3/24. // Copyright (c) 2015年 sunger. All rights reserved. // import UIKit class FirstViewController: UIViewController { @IBOutlet weak var tab1: UITabBarItem! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. //这里修改图片颜色,使得图片和原图颜色一致 tab1.image=UIImage(named: "tab1")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); //这里修改字体颜色 var attributes = [NSForegroundColorAttributeName: UIColor(red: 220.0/255.0,green: 104.0/255.0,blue: 1.0/255.0,alpha: 1.0),NSFontAttributeName: UIFont(name: "Heiti SC",size: 24.0)!] tab1.setTitleTextAttributes(attributes,forState: UIControlState.Selected) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
主要参考官方api和如下几篇文章;
1.修改图片颜色
2.修改字体颜色
http://www.cnblogs.com/xgbzsc/archive/2013/01/16/2862611.html
http://blog.csdn.net/willyang519/article/details/38855719
http://stackoverflow.com/questions/7810563/how-do-you-use-settitletextattributesforstate-in-uibaritem
最后实现如下效果