自定義 UITabBarController tabbar 選中顏色

我們需要了解下面幾個(gè)名詞

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

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

navigationController?.tabBarItem
navigationController?.tabBarController?.tabBar

來獲取對(duì)應(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 沒有被選擇時(shí),它展示的圖片是自動(dòng)從這個(gè)構(gòu)造方法中的image 參數(shù)生成的。

  • tabBarItem被選擇的時(shí)候,是從提供的selectedImage 自動(dòng)生成的,在沒有提供selectedImage時(shí),是根據(jù)image屬性自動(dòng)生成的

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

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

解決方法##

  • 如果你想要通過兩張圖片來解決自定義tabBar的顯示,必須要實(shí)現(xiàn)UIImageRenderingModeAlwaysOriginal的設(shè)置。否則,tabBarItem的被選擇時(shí)與未被選中時(shí)的顏色講被系統(tǒng)更改。(系統(tǒng)或給圖片設(shè)置默認(rèn)的填充色彩,未被選中時(shí)顏色是灰色的,被選擇時(shí)顏色是藍(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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容