我們需要了解下面幾個名詞
- ***UITabBarController ***: 標(biāo)簽試圖控制器晌纫,可以裝多個視圖控制器弱睦,屏幕下方自帶 tabBar,通過點(diǎn)擊 ***tabBarItem ***來切換視圖。
-*** UINavigationController***:導(dǎo)航視圖控制器〖丫担可以通過
navigationController?.tabBarItem
navigationController?.tabBarController?.tabBar
來獲取對應(yīng)視圖的 tabBarItem 和 tabBar
tabBar:UITabBarController視圖下方的標(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
}