UITabBarController - 选项卡控制器,与导航控制器同样,也被普遍用于各类ios应用程序。顾名思义,选项卡控制器在屏幕底部显示一系列“选显卡”,这些选项卡表示为图标和文本,用户触摸它们将在不一样的场景间切换。和UINavigationController相似,UITabBarController也能够用来控制多个页面导航,用户能够在多个视图控制器之间移动,并能够定制屏幕底部的选项卡栏。
借助屏幕底部的选项卡栏,UITabBarController没必要像UINavigationController那样以栈的方式推入和推出视图,而是创建一系列的控制器(这些控制器能够是UIViewController、UINavigationController、UITableViewController等)并将它们添加到选项卡栏,使每一个选项卡对应一个控制器。每一个场景都呈现了应用程序的一项功能,或是提供了一种查看应用程序的独特方式。UITabBarController是iOS中很经常使用的一个viewController,例如系统的闹钟程序等,QQ也是用的UITabBarController。UITabBarController一般做为整个程序的rootViewController,并且不能添加到别的container viewController中。ios
UITabBarController的View层级图编程
与导航控制器同样,选项卡控制器会为咱们处理一切。当用户触摸按钮时会在场景之间进行切换,咱们无需以编程的方式处理选项卡栏事件,也无需手工在视图控制器之间切换。app
#import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 建立窗口 self.window = [[UIWindow alloc]init]; self.window.frame = [UIScreen mainScreen].bounds; // 设置窗口的跟控制器 UITabBarController * tabbarVC = [[UITabBarController alloc]init]; // 添加子控制器 UIViewController * VC01 = [[UIViewController alloc]init]; // 设置标题 VC01.tabBarItem.title = @"精华"; // 设置默认图片 VC01.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"]; // 设置选中图片 VC01.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"]; VC01.view.backgroundColor = [UIColor yellowColor]; [tabbarVC addChildViewController:VC01]; UIViewController * VC02 = [[UIViewController alloc]init]; VC02.tabBarItem.title = @"新帖"; VC02.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"]; VC02.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_new_click_icon"]; VC02.view.backgroundColor = [UIColor redColor]; [tabbarVC addChildViewController:VC02]; UIViewController * VC03 = [[UIViewController alloc]init]; VC03.tabBarItem.title = @"关注"; VC03.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"]; VC03.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_friendTrends_click_icon"]; VC03.view.backgroundColor = [UIColor blueColor]; [tabbarVC addChildViewController:VC03]; UIViewController * VC04 = [[UIViewController alloc]init]; VC04.tabBarItem.title = @"我"; VC04.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"]; VC04.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_me_click_icon"]; VC04.view.backgroundColor = [UIColor greenColor]; [tabbarVC addChildViewController:VC04]; self.window.rootViewController = tabbarVC; // 显示窗口 [self.window makeKeyAndVisible]; return YES; }
效果图布局
可是这样在appdelegate里面进行设置会有不少缺点:优化
// 添加子控制器 UIViewController * VC01 = [[UIViewController alloc]init]; // 设置标题 VC01.tabBarItem.title = @"精华"; // 设置默认图片 VC01.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"]; UIImage * image = [UIImage imageNamed:@"tabBar_essence_click_icon"]; // 设置渲染模式 - 保持原始的渲染 image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // 设置选中图片 VC01.tabBarItem.selectedImage =image; VC01.view.backgroundColor = [UIColor yellowColor]; [self addChildViewController:VC01]
方法2:直接在图片文件夹的属性里面进行设置 - 将render as设置成original imageui
方案一:经过setTitleTextAttributes属性设置(可是这个方法很麻烦,每次设置的时候都须要写不少代码)
- setTitleTextAttributes设置的时候是经过字典设置的,因此先搞一个字典atom
// 添加子控制器 UIViewController * VC01 = [[UIViewController alloc]init]; // 设置标题 VC01.tabBarItem.title = @"精华"; // 设置默认图片 VC01.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"]; //设置选中图片 VC01.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"]; // 设置文字属性 NSMutableDictionary * attrs = [NSMutableDictionary dictionary]; attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12.0]; // 设置文字的前景色 attrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor]; [VC01.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal]; VC01.view.backgroundColor = [UIColor yellowColor]; [self addChildViewController:VC01];
- (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (void)viewDidLoad { [super viewDidLoad]; // 经过appearance统一设置UITabbarItem的文字属性 NSMutableDictionary * attrs = [NSMutableDictionary dictionary]; attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12.0]; // 设置文字大小 attrs[NSForegroundColorAttributeName] = [UIColor grayColor]; // 设置文字的前景色 NSMutableDictionary * selectedAttrs = [NSMutableDictionary dictionary]; selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName]; selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor]; UITabBarItem * item = [UITabBarItem appearance]; // 设置appearance [item setTitleTextAttributes:attrs forState:UIControlStateNormal]; [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected]; // 添加子控制器 UIViewController * VC01 = [[UIViewController alloc]init]; // 设置标题 VC01.tabBarItem.title = @"精华"; // 设置默认图片 VC01.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"]; //设置选中图片 VC01.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"]; [VC01.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal]; VC01.view.backgroundColor = [UIColor yellowColor]; [self addChildViewController:VC01]; UIViewController * VC02 = [[UIViewController alloc]init]; VC02.tabBarItem.title = @"新帖"; VC02.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"]; VC02.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_new_click_icon"]; VC02.view.backgroundColor = [UIColor redColor]; [self addChildViewController:VC02]; UIViewController * VC03 = [[UIViewController alloc]init]; VC03.tabBarItem.title = @"关注"; VC03.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"]; VC03.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_friendTrends_click_icon"]; VC03.view.backgroundColor = [UIColor blueColor]; [self addChildViewController:VC03]; UIViewController * VC04 = [[UIViewController alloc]init]; VC04.tabBarItem.title = @"我"; VC04.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"]; VC04.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_me_click_icon"]; VC04.view.backgroundColor = [UIColor greenColor]; [self addChildViewController:VC04]; }
- (void)viewDidLoad { [super viewDidLoad]; // 经过appearance统一设置UITabbarItem的文字属性 NSMutableDictionary * attrs = [NSMutableDictionary dictionary]; attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12.0]; // 设置文字大小 attrs[NSForegroundColorAttributeName] = [UIColor grayColor]; // 设置文字的前景色 NSMutableDictionary * selectedAttrs = [NSMutableDictionary dictionary]; selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName]; selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor]; UITabBarItem * item = [UITabBarItem appearance]; // 设置appearance [item setTitleTextAttributes:attrs forState:UIControlStateNormal]; [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected]; // 添加子控制器 [self setupChildVC:@"精华" andImage:@"tabBar_essence_icon" andSelectImage:@"tabBar_essence_click_icon"]; [self setupChildVC:@"新帖" andImage:@"tabBar_new_icon" andSelectImage:@"tabBar_new_click_icon"]; [self setupChildVC:@"关注" andImage:@"tabBar_friendTrends_icon" andSelectImage:@"tabBar_friendTrends_click_icon"]; [self setupChildVC:@"我" andImage:@"tabBar_me_icon" andSelectImage:@"tabBar_me_click_icon"]; } /** * 初始化子控制器 */ - (void)setupChildVC:(NSString * )title andImage:(NSString * )image andSelectImage:(NSString *)selectImage{ UIViewController * VC = [[UIViewController alloc]init]; VC.tabBarItem.title = title; VC.tabBarItem.image = [UIImage imageNamed:image]; VC.tabBarItem.selectedImage = [UIImage imageNamed:selectImage]; VC.view.backgroundColor = [UIColor greenColor]; [self addChildViewController:VC]; }
经过上面的方法设置以后tabbar的样式spa
若是想要在这个tabbar上面添加一个按钮(注意不是item,tabbaritem是图片在上,文字在下的格式),这个按钮和item样式不同,占据了和item上文字加上图片的大小。因为添加到tabbar上面的item是从左到右顺序排列的,若是是直接添加一个item,那么不能达到咱们想要的样式(固然,若是你是添加一个item的话,直接按照之前的方法就好了),这个时候能够经过自定义tabbar来实现。code
想要实现的效果orm
layoutSubviews
方法里面从新布局#import "LMHTabbar.h" @interface LMHTabbar() /** 发布按钮 */ @property (nonatomic, weak) UIButton * publishBtn; @end @implementation LMHTabbar /** * 初始化 */ - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { // 设置tabbar的子控件 UIButton * publishBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [publishBtn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal]; [publishBtn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted]; [publishBtn sizeToFit]; [self addSubview:publishBtn]; self.publishBtn = publishBtn; } return self; } /** * 重写布局子控件的方法进行布局 */ - (void)layoutSubviews{ [super layoutSubviews]; CGFloat width = self.frame.size.width; CGFloat height = self.frame.size.height; // 设置发布按钮的frame self.publishBtn.center = CGPointMake(width * 0.5, height * 0.5); // 设置其余的UITabBar的frame CGFloat buttonY = 0; CGFloat buttonW = width /5; CGFloat buttonH = height; NSInteger index = 0; for (UIView * button in self.subviews) { // 判断 - 只有是UITabBarButton的button才进行布局(也就是tabbar上面的精华、新帖、关注、我这四个按钮) 而发布按钮不是UITabBarButton,就不进行布局 // 因为UITabBar是苹果官方私有的, 因此不能直接设置 if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue; // 计算按钮的x值 CGFloat buttonX = buttonW * ((index > 1) ? (index + 1): (index)); button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH); // 索引增长 index ++; } } @end
做者:STzen 连接:https://www.jianshu.com/p/2f74a5d93faa 來源:简书 简书著做权归做者全部,任何形式的转载都请联系做者得到受权并注明出处。