免费开源的iOS开发学习平台

UITabBarController介绍:3-UITabBarItem的常用属性和方法

UITabBarItem是用于定义底部TabBar上每个按钮样式的类,UITabBarItem对象的设置是在各个子控制器中设置的,即UIViewController类中有一个UITabBarItem类的对象--tabBarItem.

UITabBarItem简介

在TabBar上每个按钮都是UITabBarItem类的对象,虽然其看上去是按钮,但UITabBarItem类并不是UIView的子类,而是NSObject的子类,因此,UIView的属性其都不具备。

@interface UITabBarItem : UIBarItem 
@interface UIBarItem : NSObject <NSCoding, UIAppearance>

UITabBarItem的常用属性和方法

UITabBarItem类中定义了有关UITabBarItem对象的操作方法以及属性,如下几个需要重点掌握。

  • UITabBarItem类的实例化方法。该方法需要传递3个参数,分别为每个UITabBarItem的底部文字描述,中间的图片以及选中后显示的图片。需要注意的是,选中状态下文字的颜色需要设置tabBar的tintColor属性。
- (instancetype)initWithTitle:(nullable NSString *)title image:(nullable UIImage *)image selectedImage:(nullable UIImage *)selectedImage;

可以查看下图中最左边的按钮的选中以及非选中状态

  • badgeValue:每个UITabBarItem对象右上角的提示信息
@property(nullable, nonatomic, copy) NSString *badgeValue;

  • badgeColor: iOS10中新加入的属性,可以定制badge的颜色,默认为红色,我们也可以修改为其他颜色。
@property (nonatomic, readwrite, copy, nullable) UIColor *badgeColor;

素材下载

本案例中使用的素材请到github下载,下载地址:九九学院素材库.

  • 使用素材名称:WeChatTabBar.zip