UITabbarController总结详解

友情链接自定义HBTabBarController(还在一点一点完善,代码简单适合快速开发初学者使用,谢谢使用并提出建议,后续会出一个swift版本并增加详细的README):https://github.com/HaiBinXiaoCheng/HBTabbarFrame

公司项目停滞不前,闲暇之余想要总结几个自己的以后用来快速开发的项目脚手架,其中之一当然是最大众的Tabbar样式。相信大部分人都会有自定义的Tabbar,但是今天既然说了事快速开发那肯定就是系统的UITabbarController,现学现卖,也是为了自己记忆
一:UITabbarController继承与UIViewController,但是有一个比需要注意的地方就是init方法的执行顺序,由于在调用Super init的时候调用了self.view所以在走完init方法之前会走viewDidLoad方法
二:基本使用方法

//创建并初始化UITabBarController
HBTabBarController *tabBarController = [[HBTabBarController alloc]init];
//设置标签栏文字和图片的颜色(默认会被填充成蓝色,不管你设置的图片颜色是什么,下面会介绍怎么图片不被填充)
tabBarController.tabBar.tintColor = [UIColor orangeColor];
//设置标签栏背景颜色
tabBarController.tabBar.barTintColor = [UIColor blackColor];
//设置标签栏风格(默认高度49)
tabBarController.tabBar.barStyle = UIBarStyleBlack;
//设置初始状态选中的下标
tabBarController.selectedIndex = 3;

//2.创建相应的子控制器(viewcontroller)
FirstViewController *firstVC = [FirstViewController new];
//导航栏文字
firstVC.navigationItem.title = @"通讯录";
//设置标签名称
firstVC.tabBarItem.title = @"通讯录";
//可以根据需求设置标签的的图标
firstVC.tabBarItem.image = [UIImage imageNamed:@"12-eye"];
//添加导航拦
UINavigationController *firstNC = [[UINavigationController alloc]initWithRootViewController:firstVC];

SecondViewController *secondVC = [SecondViewController new];
secondVC.navigationItem.title = @"朋友圈";
secondVC.tabBarItem.title = @"朋友圈";
secondVC.tabBarItem.image = [UIImage imageNamed:@"21-skull"];
UINavigationController *secondNC = [[UINavigationController alloc]initWithRootViewController:secondVC];

//3.添加到控制器
//特别注意:管理一组的控制器(最多显示五个,多余五个的话,包括第五个全部在更多模块里面,并且可以通过拖拽方式进行顺序编辑);
 NSArray *array = @[firstNC,secondNC]
tabBarController.viewControllers = array;

三:将tabBarController设置为rootViewController就可以展示基本的Tabbar了,下面说一下特别的地方

  1. 添加子控制器的方式[tabBarController addChildViewController:navC];
  2. 上面说的设置自定义的变换图片,不被其他颜色填充的方法
//设置控制器图片(使用imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal,不被系统渲染成蓝色(或者你刚才设置的tintColor颜色))
    firstVC.tabBarItem.image = [[UIImage imageNamed:@"icon_home_bottom_statist"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    firstVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"icon_home_bottom_statist_hl"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  1. TabbarItem的显示与隐藏
self.hidesBottomBarWhenPushed = YES;
self.hidesBottomBarWhenPushed = NO;
  1. 其它一下设置
c1.tabBarItem.badgeValue=@"123";//提醒数字
//设置标题的位置偏移
//@property (nonatomic, readwrite, assign) UIOffset titlePositionAdjustment;

 设置和获取标题的字体属性
- (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state;
- (nullable NSDictionary<NSString *,id> *)titleTextAttributesForState:(UIControlState)state;

设置自定义标签
 //调用这个方法会弹出一个类似上面第二张截图的控制器,我们可以交换标签的布局顺序
- (void)beginCustomizingItems:(NSArray<UITabBarItem *> *)items;
//完成标签布局
 - (BOOL)endCustomizingAnimated:(BOOL)animated;
 //是否正在自定义标签布局
 - (BOOL)isCustomizing;

设置背景图片
 //设置导航栏背景图案
 @property(nullable, nonatomic,strong) UIImage *backgroundImage;
 //设置选中一个标签时,标签背后的选中提示图案 这个会出现在设置的item图案的后面
 @property(nullable, nonatomic,strong) UIImage *selectionIndicatorImage;
 //设置阴影的背景图案
 @property(nullable, nonatomic,strong) UIImage *shadowImage

 tabbar标签的属性
 //设置标签item的位置模式
 @property(nonatomic) UITabBarItemPositioning itemPositioning;
 //枚举如下
 typedef NS_ENUM(NSInteger, UITabBarItemPositioning) {
 UITabBarItemPositioningAutomatic,//自动
 UITabBarItemPositioningFill,//充满
UITabBarItemPositioningCentered,//中心
 } NS_ENUM_AVAILABLE_IOS(7_0);
 //设置item宽度
 @property(nonatomic) CGFloat itemWidth;
 //设置item间距
 @property(nonatomic) CGFloat itemSpacing;

 设置tabbar的风格和透明
 //风格 分黑白两种
 @property(nonatomic) UIBarStyle barStyle;
//是否透明效果
 @property(nonatomic,getter=isTranslucent) BOOL translucent;

 delegate
//选中标签时调用
 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
 //将要开始编辑标签时
 - (void)tabBar:(UITabBar *)tabBar willBeginCustomizingItems:(NSArray<UITabBarItem *> *)items;          
//已经开始编辑标签时
 - (void)tabBar:(UITabBar *)tabBar didBeginCustomizingItems:(NSArray<UITabBarItem *> *)items;
 //将要进入编辑状态时
 - (void)tabBar:(UITabBar *)tabBar willEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed;
 //已经进入编辑状态时
 - (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed;

猜你喜欢

转载自blog.csdn.net/u012918868/article/details/80891679
今日推荐