UITabBarController

本文详细介绍了iOS开发中UITabBarController的使用,包括其属性如viewControllers、tabBar以及如何设置选中状态。同时,讲解了UITabBarItem的定制,如通过tabBarItem设置底部按钮样式,并探讨了UITabBar的属性如tintColor和barTintColor。此外,还提到了代理方法的应用,用于控制子控制器的切换。
摘要由CSDN通过智能技术生成

1 UITabBarController的常用属性和方法

一般来说,目前市面上的App很多都是由一个UITabBarController管理着4~5个UINavigationController,然后每个UINavigationController又管理着一组UIViewController对象或者其子类对象(如UITableViewController、UICollectionViewController等)。

在UITabBarController类中,有两个核心属性,分别为viewControllers和tabBar,其分别管理UITabBarController的子控制器以及底部菜单栏的样式。

// viewControllers:用于存储UITabBarController所需要管理的子控制器,通常这些子控制器是UINavigationController类的对象。
@property(nullable, nonatomic, copy) NSArray<__kindof UIViewController *> *viewController;
// tabBar:用于定制tabBar的整体外观样式,例如,背景颜色、背景图片等,需要注意的是:tabBar上每个按钮的样式定制并不由tabBar决定,这个与导航栏navigationBar是类似的。
@property(nonatomic, readonly) UITabBar *tabBar;
// selectedViewController:设置被选中的子控制器,被选中的子控制器会显示被选中的状态。
@property(nullable, nonatomic, assign) __kindof UIViewController (selectedViewController;
// selectedIndex:根据序号设置被选中的子控制器。
@property(nonatomic) NSUInteger selectedIndex;
3.UIViewController类中的tabBarItem与tabBarController
UITabBarController类中也对UIViewController类的属性进行了扩展,增加了tabBarItem这个关键属性,因此,每个子控制器的UITabBarItem样式,其实是在子控制器对象中进行设置的,这样保证了定制的灵活性。
另外,tabBarController属性使每个子控制器都可以获取到管理它的UITabBarController对象。
@interface UIViewController (UITabBarControllerItem)
@property(nullresettable, nonatomic, strong) UITabVarItem *tabBarItem;
@property(nullable, nonatomic, readonly, strong) UITabBarController *tabBarController;
@end

2 UITabBar的常用属性和方法

UITabBar类对象tabBar是UITabBarController中的一个属性,其负责定义底部功能栏的整体外观样式,但每个菜单按钮的样式并不是由UITabBar类对象控制的,而是有各个子控制器中的tabBarItem(UITabBarItem类)属性决定的。

// tintColor:tabBar整体渲染的颜色。
@property(null_resettable, nonatomic, strong) UIColor *tintColor;
// barTintColor:tabBar的整体背景颜色。
@property(nullable, nonatomic, strong) UIColor *barTintColor;
// backgroundImage:为tabBar设置背景图片。
@property(nullable, nonatomic, strong) UIColor *backgroundImage;
// barStyle:tabBar的整体风格样式,默认为白色的,可以改为黑色。
@property(nonatomic) UIBarStyle barStyle;
typedef NS_ENUM(NSInteger, UIBarStyle) {
    UIBarStyleDefault = 0,
    UIBarStyleBlack = 1,
}
// items:获取tabBar上面的所有UITabBarItem类的对象,即底部菜单栏上面的按钮,可以对其样式进行修改。
@property(nullable, nonatomic, copy) NSArray<UITabBarItem *> *items;

3 UITabBarItem的常用属性和方法

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

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

// 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:iOS 10中新加入的属性,可以定制badge的颜色,默认为红色,也可以修改为其他颜色
@property(nonatomic, readwrite, copy, nullable) UIColor *badgeColor;

5 UITabBarController的代理协议

// 当切换子控制器时调用。可以用于切换控制器时准备界面数据
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
// 设置子控制器能否被选中。
-(BOOL)taBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值