我们需要了解下面几个名词
- ***UITabBarController ***: 标签试图控制器,可以装多个视图控制器,屏幕下方自带 tabBar,通过点击 ***tabBarItem ***来切换视图。
-*** UINavigationController***:导航视图控制器。可以通过
navigationController?.tabBarItem
navigationController?.tabBarController?.tabBar
来获取对应视图的 tabBarItem 和 tabBar
-
tabBar:UITabBarController视图下方的标签栏,用来展示tabBarItem
-
tabBarItem: 负责显示标签按钮的样式
1. 进入正题
tabBarItem 是 UITabBarItem ,继承UIBarItem。
我们可以设置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的构造方法
前面一大段注释的意思是
-
tabBarItem 没有被选择时,它展示的图片是自动从这个构造方法中的image 参数生成的。
-
tabBarItem被选择的时候,是从提供的selectedImage 自动生成的,在没有提供selectedImage时,是根据image属性自动生成的
-
如果防止系统绘制图片,给image设置UIImageRenderingModeAlwaysOriginal 属性
(ps: 设置UIImageRenderingModeAlwaysOriginal的方法如下)var image = UIImage(named: "Image") image = image?.imageWithRenderingMode(.AlwaysOriginal)
解决方法##
-
如果你想要通过两张图片来解决自定义tabBar的显示,必须要实现UIImageRenderingModeAlwaysOriginal的设置。否则,tabBarItem的被选择时与未被选中时的颜色讲被系统更改。(系统或给图片设置默认的填充色彩,未被选中时颜色是灰色的,被选择时颜色是蓝色的)
-
如果你想用一张图片,通过修改系统的填充颜色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
}
网友评论