自定義 UITabBarController tabbar 選中顏色

我們需要了解下面幾個名詞

  • ***UITabBarController ***: 標(biāo)簽試圖控制器晌纫,可以裝多個視圖控制器弱睦,屏幕下方自帶 tabBar,通過點(diǎn)擊 ***tabBarItem ***來切換視圖。

-*** UINavigationController***:導(dǎo)航視圖控制器〖丫担可以通過

navigationController?.tabBarItem
navigationController?.tabBarController?.tabBar

來獲取對應(yīng)視圖的 tabBarItemtabBar

  • tabBarUITabBarController視圖下方的標(biāo)簽欄,用來展示tabBarItem

  • tabBarItem: 負(fù)責(zé)顯示標(biāo)簽按鈕的樣式

1. 進(jìn)入正題

tabBarItem 是 UITabBarItem 凡桥,繼承UIBarItem蟀伸。

我們可以設(shè)置tabBarItem的圖片

/* The unselected image is autogenerated from the image argument. The selected image
 is autogenerated from the selectedImage if provided and the image argument otherwise.
 To prevent system coloring, provide images with UIImageRenderingModeAlwaysOriginal (see UIImage.h)
 */
public convenience init(title: String?, image: UIImage?, tag: Int)

這是cocoa框架提供的 tabBarItem的構(gòu)造方法
前面一大段注釋的意思是

  • tabBarItem 沒有被選擇時,它展示的圖片是自動從這個構(gòu)造方法中的image 參數(shù)生成的唬血。

  • tabBarItem被選擇的時候望蜡,是從提供的selectedImage 自動生成的,在沒有提供selectedImage時拷恨,是根據(jù)image屬性自動生成的

  • 如果防止系統(tǒng)繪制圖片,給image設(shè)置UIImageRenderingModeAlwaysOriginal 屬性
    (ps: 設(shè)置UIImageRenderingModeAlwaysOriginal的方法如下)

      var image = UIImage(named: "Image")
      image = image?.imageWithRenderingMode(.AlwaysOriginal)
    

解決方法##

  • 如果你想要通過兩張圖片來解決自定義tabBar的顯示谢肾,必須要實(shí)現(xiàn)UIImageRenderingModeAlwaysOriginal的設(shè)置腕侄。否則,tabBarItem的被選擇時與未被選中時的顏色講被系統(tǒng)更改芦疏。(系統(tǒng)或給圖片設(shè)置默認(rèn)的填充色彩冕杠,未被選中時顏色是灰色的,被選擇時顏色是藍(lán)色的)

  • 如果你想用一張圖片酸茴,通過修改系統(tǒng)的填充顏色tintColor來完成目的分预。也是可以的。通過下面的方法薪捍。

tabBarController?.tabBar.tintColor = UIColor.redColor()

在這里附上 UIBarItem UITabBarItem的公開屬性 方便查看

*** UIBarItem*** 的屬性

public class UIBarItem : NSObject, NSCoding, UIAppearance {
public init()
public init?(coder aDecoder: NSCoder)

public var enabled: Bool // default is YES
public var title: String? // default is nil
public var image: UIImage? // default is nil
@available(iOS 5.0, *)
public var landscapeImagePhone: UIImage? // default is nil
public var imageInsets: UIEdgeInsets // default is UIEdgeInsetsZero
@available(iOS 5.0, *)
public var landscapeImagePhoneInsets: UIEdgeInsets // default is UIEdgeInsetsZero. These insets apply only when the landscapeImagePhone property is set.
public var tag: Int // default is 0

/* You may specify the font, text color, and shadow properties for the title in the text attributes dictionary, using the keys found in NSAttributedString.h.
 */
@available(iOS 5.0, *)
public func setTitleTextAttributes(attributes: [String : AnyObject]?, forState state: UIControlState)
@available(iOS 5.0, *)
public func titleTextAttributesForState(state: UIControlState) -> [String : AnyObject]?
}

這是 UITabBarItem的屬性和方法

public class UITabBarItem : UIBarItem {

public init()
public init?(coder aDecoder: NSCoder)
/* The unselected image is autogenerated from the image argument. The selected image
 is autogenerated from the selectedImage if provided and the image argument otherwise.
 To prevent system coloring, provide images with UIImageRenderingModeAlwaysOriginal (see UIImage.h)
 */
public convenience init(title: String?, image: UIImage?, tag: Int)
@available(iOS 7.0, *)
public convenience init(title: String?, image: UIImage?, selectedImage: UIImage?)
public convenience init(tabBarSystemItem systemItem: UITabBarSystemItem, tag: Int)

@available(iOS 7.0, *)
public var selectedImage: UIImage?

public var badgeValue: String? // default is nil

/* To set item label text attributes use the appearance selectors available on the superclass, UIBarItem.

   Use the following to tweak the relative position of the label within the tab button (for handling visual centering corrections if needed because of custom text attributes)
 */
@available(iOS 5.0, *)
public var titlePositionAdjustment: UIOffset
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末笼痹,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子酪穿,更是在濱河造成了極大的恐慌凳干,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件被济,死亡現(xiàn)場離奇詭異危融,居然都是意外死亡替久,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來贼陶,“玉大人,你說我怎么就攤上這事晃财∶漯模” “怎么了?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵畏陕,是天一觀的道長配乓。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么犹芹? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任崎页,我火速辦了婚禮,結(jié)果婚禮上腰埂,老公的妹妹穿的比我還像新娘飒焦。我一直安慰自己,他們只是感情好屿笼,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布牺荠。 她就那樣靜靜地躺著,像睡著了一般驴一。 火紅的嫁衣襯著肌膚如雪休雌。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天肝断,我揣著相機(jī)與錄音杈曲,去河邊找鬼。 笑死胸懈,一個胖子當(dāng)著我的面吹牛担扑,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播趣钱,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼涌献,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了首有?” 一聲冷哼從身側(cè)響起燕垃,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎绞灼,沒想到半個月后利术,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡低矮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年印叁,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片军掂。...
    茶點(diǎn)故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡轮蜕,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蝗锥,到底是詐尸還是另有隱情跃洛,我是刑警寧澤,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布终议,位于F島的核電站汇竭,受9級特大地震影響葱蝗,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜细燎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一两曼、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧玻驻,春花似錦悼凑、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至嗤锉,卻和暖如春渔欢,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背档冬。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工膘茎, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人酷誓。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像态坦,于是被迫代替她去往敵國和親盐数。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容