iOS开发项目篇—27自定义UITabBar
一、自定义
思路:
(1)新建一个继承自UITabBar的类,自定义一个UITabBar
(2)用自定义的UITabBar换掉系统的UItabBar(使用了KVC)
(3)监听控制器的切换,只要控制器一切换,就调用代理方法强制重新布局子控件(内部会调用layoutSubviews)。
YYTabBar.m文件代码:
1 // 2 // YYTabBar.m 3 // 4 5 #import "YYTabBar.h" 6 7 @interface YYTabBar() 8 @property (nonatomic, weak) UIButton *plusButton; 9 @end 10 11 @implementation YYTabBar 12 13 - (id)initWithFrame:(CGRect)frame 14 { 15 self = [super initWithFrame:frame]; 16 if (self) { 17 // 添加加号按钮 18 [self setupPlusButton]; 19 } 20 return self; 21 } 22 23 /** 24 * 添加加号按钮 25 */ 26 - (void)setupPlusButton 27 { 28 UIButton *plusButton = [[UIButton alloc] init]; 29 // 设置背景 30 [plusButton setBackgroundImage:[UIImage imageWithName:@"tabbar_compose_button"] forState:UIControlStateNormal]; 31 [plusButton setBackgroundImage:[UIImage imageWithName:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted]; 32 // 设置图标 33 [plusButton setImage:[UIImage imageWithName:@"tabbar_compose_icon_add"] forState:UIControlStateNormal]; 34 [plusButton setImage:[UIImage imageWithName:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted]; 35 [plusButton addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside]; 36 // 添加 37 [self addSubview:plusButton]; 38 self.plusButton = plusButton; 39 } 40 41 - (void)plusClick 42 { 43 YYLog(@"plusClick----"); 44 } 45 46 /** 47 * 布局子控件 48 */ 49 - (void)layoutSubviews 50 { 51 [super layoutSubviews]; 52 53 // 设置plusButton的frame 54 [self setupPlusButtonFrame]; 55 56 // 设置所有tabbarButton的frame 57 [self setupAllTabBarButtonsFrame]; 58 } 59 60 /** 61 * 设置所有plusButton的frame 62 */ 63 - (void)setupPlusButtonFrame 64 { 65 self.plusButton.size = self.plusButton.currentBackgroundImage.size; 66 self.plusButton.center = CGPointMake(self.width * 0.5, self.height * 0.5); 67 } 68 69 /** 70 * 设置所有tabbarButton的frame 71 */ 72 - (void)setupAllTabBarButtonsFrame 73 { 74 int index = 0; 75 76 // 遍历所有的button 77 for (UIView *tabBarButton in self.subviews) { 78 // 如果不是UITabBarButton, 直接跳过 79 if (![tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue; 80 81 // 根据索引调整位置 82 [self setupTabBarButtonFrame:tabBarButton atIndex:index]; 83 84 // 遍历UITabBarButton中的所有子控件 85 [self setupTabBarButtonTextColor:tabBarButton atIndex:index]; 86 87 // 索引增加 88 index++; 89 } 90 } 91 92 /** 93 * 设置某个按钮的文字颜色 94 * 95 * @param tabBarButton 需要设置的按钮 96 * @param index 按钮所在的索引 97 */ 98 - (void)setupTabBarButtonTextColor:(UIView *)tabBarButton atIndex:(int)index 99 { 100 // 选中按钮的索引 101 int selectedIndex = [self.items indexOfObject:self.selectedItem]; 102 103 for (UILabel *label in tabBarButton.subviews) { 104 // 说明不是个Label 105 if (![label isKindOfClass:[UILabel class]]) continue; 106 107 // 设置字体 108 label.font = [UIFont systemFontOfSize:10]; 109 if (selectedIndex == index) { // 说明这个Button选中, 设置label颜色为橙色 110 label.textColor = [UIColor orangeColor]; 111 } else { // 说明这个Button没有选中, 设置label颜色为黑色 112 label.textColor = [UIColor blackColor]; 113 } 114 } 115 } 116 117 /** 118 * 设置某个按钮的frame 119 * 120 * @param tabBarButton 需要设置的按钮 121 * @param index 按钮所在的索引 122 */ 123 - (void)setupTabBarButtonFrame:(UIView *)tabBarButton atIndex:(int)index 124 { 125 // 计算button的尺寸 126 CGFloat buttonW = self.width / (self.items.count + 1); 127 CGFloat buttonH = self.height; 128 129 tabBarButton.width = buttonW; 130 tabBarButton.height = buttonH; 131 if (index >= 2) { 132 tabBarButton.x = buttonW * (index + 1); 133 } else { 134 tabBarButton.x = buttonW * index; 135 } 136 tabBarButton.y = 0; 137 } 138 @end
在YYTabBarViewController.m文件中使用自定义的UITabBar
1 // 2 // YYTabBarViewController.m 3 // 4 5 #import "YYTabBarViewController.h" 6 #import "YYHomeTableViewController.h" 7 #import "YYDiscoverViewController.h" 8 #import "YYMessageViewController.h" 9 #import "YYProfileViewController.h" 10 #import "UIImage+Extension.h" 11 #import "YYNavigationViewController.h" 12 #import "YYTabBar.h" 13 14 @interface YYTabBarViewController ()<UITabBarControllerDelegate> 15 16 @end 17 18 @implementation YYTabBarViewController 19 20 21 - (void)viewDidLoad 22 { 23 [super viewDidLoad]; 24 //添加四个子控制器 25 YYHomeTableViewController *home=[[YYHomeTableViewController alloc]init]; 26 [self addOneChildVc:home title:@"首页" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_selected"]; 27 28 29 YYMessageViewController *message=[[YYMessageViewController alloc]init]; 30 [self addOneChildVc:message title:@"消息" imageName:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_selected"]; 31 32 YYDiscoverViewController *discover=[[YYDiscoverViewController alloc]init]; 33 [self addOneChildVc:discover title:@"发现" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_selected"]; 34 35 YYProfileViewController *profile=[[YYProfileViewController alloc]init]; 36 [self addOneChildVc:profile title:@"我" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_selected"]; 37 38 39 // 调整tabbar 40 YYTabBar *customTabBar = [[YYTabBar alloc] init]; 41 customTabBar.backgroundImage = [UIImage imageWithName:@"tabbar_background"]; 42 customTabBar.selectionIndicatorImage = [UIImage imageWithName:@"navigationbar_button_background"]; 43 // 更换系统自带的tabbar 44 [self setValue:customTabBar forKeyPath:@"tabBar"]; 45 46 // 设置代理(监听控制器的切换, 控制器一旦切换了子控制器,就会调用代理的tabBarController:didSelectViewController:) 47 self.delegate = self; 48 49 } 50 51 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController 52 { 53 // 强制重新布局子控件(内部会调用layouSubviews) 54 [self.tabBar setNeedsLayout]; 55 } 56 57 /** 58 * 添加一个子控制器 59 * 60 * @param childVC 子控制对象 61 * @param title 标题 62 * @param imageName 图标 63 * @param selectedImageName 选中时的图标 64 */ 65 -(void)addOneChildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName 66 { 67 //随机设置子控制器的背景颜色 68 // childVc.view.backgroundColor=YYRandomColor; 69 70 //设置标题 71 childVc.title=title; //相当于设置了后两者的标题 72 // childVc.navigationItem.title=title;//设置导航栏的标题 73 // childVc.tabBarItem.title=title;//设置tabbar上面的标题 74 75 //设置图标 76 childVc.tabBarItem.image=[UIImage imageWithName:imageName]; 77 //设置选中时的图标 78 UIImage *selectedImage=[UIImage imageWithName:selectedImageName]; 79 80 81 if (iOS7) { 82 // 声明这张图片用原图(别渲染) 83 selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 84 } 85 childVc.tabBarItem.selectedImage = selectedImage; 86 87 // 添加为tabbar控制器的子控制器 88 YYNavigationViewController *nav=[[YYNavigationViewController alloc]initWithRootViewController:childVc]; 89 90 [self addChildViewController:nav]; 91 92 } 93 94 95 // 在iOS7中, 会对selectedImage的图片进行再次渲染为蓝色 96 // 要想显示原图, 就必须得告诉它: 不要渲染 97 98 // Xcode的插件安装路径: /Users/用户名/Library/Application Support/Developer/Shared/Xcode/Plug-ins 99 @end
实现效果(iOS7中)
关键代码:更换系统自带的tabbar [self setValue:customTabBar forKeyPath:@"tabBar"];
说明:不能直接使用self.tabBar的方式调用set方法进行更换,因为这个属性是只读的,这里的实现是直接使用KVC把它内部的下划线常量给修改了。
二、完善
问题说明:
(1)上面的代码下方的UITabBar是透明的。因为ios6中的背景图片是白色的,而ios7中的背景图片是透明的。有两种解决方式,一种是在mainbundle中直接把ios7对应的那张图片素材删除即可,但是这种方法会导致ios7中的穿透效果没有。第二种解决方式,判断当前系统版本,如果当前是ios7则不需要设置。
(2)子控件的frame没必要每次都设置,只需要设置一次就可以了。切换控制器的目的只有一个,就是改变默认和选中状态下文字的颜色。
完善后的代码:
YYTabBar.m文件
1 // 2 // YYTabBar.m 3 // 4 5 #import "YYTabBar.h" 6 7 @interface YYTabBar() 8 @property (nonatomic, weak) UIButton *plusButton; 9 @end 10 11 @implementation YYTabBar 12 13 - (id)initWithFrame:(CGRect)frame 14 { 15 self = [super initWithFrame:frame]; 16 if (self) { 17 if (!iOS7) { 18 self.backgroundImage = [UIImage imageWithName:@"tabbar_background"]; 19 } 20 self.selectionIndicatorImage = [UIImage imageWithName:@"navigationbar_button_background"]; 21 // 添加加号按钮 22 [self setupPlusButton]; 23 } 24 return self; 25 } 26 27 /** 28 * 添加加号按钮 29 */ 30 - (void)setupPlusButton 31 { 32 UIButton *plusButton = [[UIButton alloc] init]; 33 // 设置背景 34 [plusButton setBackgroundImage:[UIImage imageWithName:@"tabbar_compose_button"] forState:UIControlStateNormal]; 35 [plusButton setBackgroundImage:[UIImage imageWithName:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted]; 36 // 设置图标 37 [plusButton setImage:[UIImage imageWithName:@"tabbar_compose_icon_add"] forState:UIControlStateNormal]; 38 [plusButton setImage:[UIImage imageWithName:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted]; 39 [plusButton addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside]; 40 // 添加 41 [self addSubview:plusButton]; 42 self.plusButton = plusButton; 43 } 44 45 - (void)plusClick 46 { 47 YYLog(@"plusClick----"); 48 } 49 50 /** 51 * 布局子控件 52 */ 53 - (void)layoutSubviews 54 { 55 [super layoutSubviews]; 56 57 // 设置plusButton的frame 58 [self setupPlusButtonFrame]; 59 60 // 设置所有tabbarButton的frame 61 [self setupAllTabBarButtonsFrame]; 62 } 63 64 /** 65 * 设置所有plusButton的frame 66 */ 67 - (void)setupPlusButtonFrame 68 { 69 self.plusButton.size = self.plusButton.currentBackgroundImage.size; 70 self.plusButton.center = CGPointMake(self.width * 0.5, self.height * 0.5); 71 } 72 73 /** 74 * 设置所有tabbarButton的frame 75 */ 76 - (void)setupAllTabBarButtonsFrame 77 { 78 int index = 0; 79 80 // 遍历所有的button 81 for (UIView *tabBarButton in self.subviews) { 82 // 如果不是UITabBarButton, 直接跳过 83 if (![tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue; 84 85 // 根据索引调整位置 86 [self setupTabBarButtonFrame:tabBarButton atIndex:index]; 87 // 88 // // 遍历UITabBarButton中的所有子控件 89 // [self setupTabBarButtonTextColor:tabBarButton atIndex:index]; 90 91 // 索引增加 92 index++; 93 } 94 } 95 96 ///** 97 // * 设置某个按钮的文字颜色 98 // * 99 // * @param tabBarButton 需要设置的按钮 100 // * @param index 按钮所在的索引 101 // */ 102 //- (void)setupTabBarButtonTextColor:(UIView *)tabBarButton atIndex:(int)index 103 //{ 104 // // 选中按钮的索引 105 // int selectedIndex = [self.items indexOfObject:self.selectedItem]; 106 // 107 // for (UILabel *label in tabBarButton.subviews) { 108 // // 说明不是个Label 109 // if (![label isKindOfClass:[UILabel class]]) continue; 110 // 111 // // 设置字体 112 // label.font = [UIFont systemFontOfSize:10]; 113 // if (selectedIndex == index) { // 说明这个Button选中, 设置label颜色为橙色 114 // label.textColor = [UIColor orangeColor]; 115 // } else { // 说明这个Button没有选中, 设置label颜色为黑色 116 // label.textColor = [UIColor blackColor]; 117 // } 118 // } 119 //} 120 121 /** 122 * 设置某个按钮的frame 123 * 124 * @param tabBarButton 需要设置的按钮 125 * @param index 按钮所在的索引 126 */ 127 - (void)setupTabBarButtonFrame:(UIView *)tabBarButton atIndex:(int)index 128 { 129 // 计算button的尺寸 130 CGFloat buttonW = self.width / (self.items.count + 1); 131 CGFloat buttonH = self.height; 132 133 tabBarButton.width = buttonW; 134 tabBarButton.height = buttonH; 135 if (index >= 2) { 136 tabBarButton.x = buttonW * (index + 1); 137 } else { 138 tabBarButton.x = buttonW * index; 139 } 140 tabBarButton.y = 0; 141 } 142 @end
YYTabBarViewController.m文件
1 // 2 // YYTabBarViewController.m 3 // 4 5 #import "YYTabBarViewController.h" 6 #import "YYHomeTableViewController.h" 7 #import "YYDiscoverViewController.h" 8 #import "YYMessageViewController.h" 9 #import "YYProfileViewController.h" 10 #import "UIImage+Extension.h" 11 #import "YYNavigationViewController.h" 12 #import "YYTabBar.h" 13 14 @interface YYTabBarViewController ()<UITabBarControllerDelegate> 15 16 @end 17 18 @implementation YYTabBarViewController 19 20 21 - (void)viewDidLoad 22 { 23 [super viewDidLoad]; 24 //添加四个子控制器 25 YYHomeTableViewController *home=[[YYHomeTableViewController alloc]init]; 26 [self addOneChildVc:home title:@"首页" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_selected"]; 27 28 29 YYMessageViewController *message=[[YYMessageViewController alloc]init]; 30 [self addOneChildVc:message title:@"消息" imageName:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_selected"]; 31 32 YYDiscoverViewController *discover=[[YYDiscoverViewController alloc]init]; 33 [self addOneChildVc:discover title:@"发现" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_selected"]; 34 35 YYProfileViewController *profile=[[YYProfileViewController alloc]init]; 36 [self addOneChildVc:profile title:@"我" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_selected"]; 37 38 39 // 调整tabbar 40 YYTabBar *customTabBar = [[YYTabBar alloc] init]; 41 // 更换系统自带的tabbar 42 [self setValue:customTabBar forKeyPath:@"tabBar"]; 43 44 // // 设置代理(监听控制器的切换, 控制器一旦切换了子控制器,就会调用代理的tabBarController:didSelectViewController:) 45 // self.delegate = self; 46 47 } 48 49 //- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController 50 //{ 51 // // 强制重新布局子控件(内部会调用layouSubviews) 52 // [self.tabBar setNeedsLayout]; 53 //} 54 55 56 /** 57 * 添加一个子控制器 58 * 59 * @param childVC 子控制对象 60 * @param title 标题 61 * @param imageName 图标 62 * @param selectedImageName 选中时的图标 63 */ 64 -(void)addOneChildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName 65 { 66 //随机设置子控制器的背景颜色 67 // childVc.view.backgroundColor=YYRandomColor; 68 69 //设置标题 70 childVc.title=title; //相当于设置了后两者的标题 71 // childVc.navigationItem.title=title;//设置导航栏的标题 72 // childVc.tabBarItem.title=title;//设置tabbar上面的标题 73 74 //设置图标 75 childVc.tabBarItem.image=[UIImage imageWithName:imageName]; 76 //设置选中时的图标 77 UIImage *selectedImage=[UIImage imageWithName:selectedImageName]; 78 79 //设置tabBarItem普通状态下文字的颜色 80 NSMutableDictionary *textAttrs=[NSMutableDictionary dictionary]; 81 textAttrs[UITextAttributeTextColor]=[UIColor blackColor]; 82 textAttrs[UITextAttributeFont]=[UIFont systemFontOfSize:10]; 83 [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal]; 84 85 //设置tabBarItem普通状态下文字的颜色 86 NSMutableDictionary *selectedtextAttrs=[NSMutableDictionary dictionary]; 87 selectedtextAttrs[UITextAttributeTextColor]=[UIColor orangeColor]; 88 [childVc.tabBarItem setTitleTextAttributes:selectedtextAttrs forState:UIControlStateSelected]; 89 90 if (iOS7) { 91 // 声明这张图片用原图(别渲染) 92 selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 93 } 94 childVc.tabBarItem.selectedImage = selectedImage; 95 96 // 添加为tabbar控制器的子控制器 97 YYNavigationViewController *nav=[[YYNavigationViewController alloc]initWithRootViewController:childVc]; 98 99 [self addChildViewController:nav]; 100 101 } 102 103 104 // 在iOS7中, 会对selectedImage的图片进行再次渲染为蓝色 105 // 要想显示原图, 就必须得告诉它: 不要渲染 106 107 // Xcode的插件安装路径: /Users/用户名/Library/Application Support/Developer/Shared/Xcode/Plug-ins 108 @end
三、加号按钮的点击事件
使用代理方法,当点击加号按钮的时候,通知UITabBarController以模态的方式弹出控制器。
实现代码:
1.设置代理
YYTabBar.h文件
1 // 2 // YYTabBar.h 3 // 4 5 #import <UIKit/UIKit.h> 6 7 @class YYTabBar; 8 @protocol YYTabBarDelegate <NSObject> 9 -(void)tabBarDidClickedPlusButton:(YYTabBar *)tabBar; 10 @end 11 12 @interface YYTabBar : UITabBar 13 @property(nonatomic,weak)id<YYTabBarDelegate> delegate; 14 @end
YYTabBar.m文件
1 // 2 // YYTabBar.m 3 // 4 5 #import "YYTabBar.h" 6 7 @interface YYTabBar() 8 @property (nonatomic, weak) UIButton *plusButton; 9 @end 10 11 @implementation YYTabBar 12 13 - (id)initWithFrame:(CGRect)frame 14 { 15 self = [super initWithFrame:frame]; 16 if (self) { 17 if (!iOS7) { 18 self.backgroundImage = [UIImage imageWithName:@"tabbar_background"]; 19 } 20 self.selectionIndicatorImage = [UIImage imageWithName:@"navigationbar_button_background"]; 21 // 添加加号按钮 22 [self setupPlusButton]; 23 } 24 return self; 25 } 26 27 28 /** 29 * 添加加号按钮 30 */ 31 - (void)setupPlusButton 32 { 33 UIButton *plusButton = [[UIButton alloc] init]; 34 // 设置背景 35 [plusButton setBackgroundImage:[UIImage imageWithName:@"tabbar_compose_button"] forState:UIControlStateNormal]; 36 [plusButton setBackgroundImage:[UIImage imageWithName:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted]; 37 // 设置图标 38 [plusButton setImage:[UIImage imageWithName:@"tabbar_compose_icon_add"] forState:UIControlStateNormal]; 39 [plusButton setImage:[UIImage imageWithName:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted]; 40 [plusButton addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside]; 41 // 添加 42 [self addSubview:plusButton]; 43 self.plusButton = plusButton; 44 } 45 46 - (void)plusClick 47 { 48 //设置代理 49 if ([self.delegate respondsToSelector:@selector(tabBarDidClickedPlusButton:)]) { 50 [self.delegate tabBarDidClickedPlusButton:self]; 51 } 52 } 53 54 /** 55 * 布局子控件 56 */ 57 - (void)layoutSubviews 58 { 59 [super layoutSubviews]; 60 61 // 设置plusButton的frame 62 [self setupPlusButtonFrame]; 63 64 // 设置所有tabbarButton的frame 65 [self setupAllTabBarButtonsFrame]; 66 } 67 68 /** 69 * 设置所有plusButton的frame 70 */ 71 - (void)setupPlusButtonFrame 72 { 73 self.plusButton.size = self.plusButton.currentBackgroundImage.size; 74 self.plusButton.center = CGPointMake(self.width * 0.5, self.height * 0.5); 75 } 76 77 /** 78 * 设置所有tabbarButton的frame 79 */ 80 - (void)setupAllTabBarButtonsFrame 81 { 82 int index = 0; 83 84 // 遍历所有的button 85 for (UIView *tabBarButton in self.subviews) { 86 // 如果不是UITabBarButton, 直接跳过 87 if (![tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue; 88 89 // 根据索引调整位置 90 [self setupTabBarButtonFrame:tabBarButton atIndex:index]; 91 92 // 索引增加 93 index++; 94 } 95 } 96 97 98 /** 99 * 设置某个按钮的frame 100 * 101 * @param tabBarButton 需要设置的按钮 102 * @param index 按钮所在的索引 103 */ 104 - (void)setupTabBarButtonFrame:(UIView *)tabBarButton atIndex:(int)index 105 { 106 // 计算button的尺寸 107 CGFloat buttonW = self.width / (self.items.count + 1); 108 CGFloat buttonH = self.height; 109 110 tabBarButton.width = buttonW; 111 tabBarButton.height = buttonH; 112 if (index >= 2) { 113 tabBarButton.x = buttonW * (index + 1); 114 } else { 115 tabBarButton.x = buttonW * index; 116 } 117 tabBarButton.y = 0; 118 } 119 @end
2.新建一个发送消息的控制器,其继承自UIViewController
YYComposeViewController.m文件
1 // 2 // YYComposeViewController.m 3 // 4 5 #import "YYComposeViewController.h" 6 7 @interface YYComposeViewController () 8 9 @end 10 11 @implementation YYComposeViewController 12 13 14 - (void)viewDidLoad 15 { 16 [super viewDidLoad]; 17 self.title=@"发消息"; 18 self.view.backgroundColor=[UIColor yellowColor]; 19 self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel)]; 20 self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"发送" style:UIBarButtonItemStyleBordered target:self action:@selector(send)]; 21 self.navigationItem.rightBarButtonItem.enabled=NO; 22 } 23 24 -(void)send 25 { 26 YYLog(@"发送----"); 27 } 28 29 -(void)cancel 30 { 31 [self dismissViewControllerAnimated:YES completion:nil]; 32 } 33 @end
3.实现代理方法,监听加号按钮的点击事件
YYTabBarViewController.m文件
1 // 2 // YYTabBarViewController.m 3 // 4 5 #import "YYTabBarViewController.h" 6 #import "YYHomeTableViewController.h" 7 #import "YYDiscoverViewController.h" 8 #import "YYMessageViewController.h" 9 #import "YYProfileViewController.h" 10 #import "UIImage+Extension.h" 11 #import "YYNavigationViewController.h" 12 #import "YYTabBar.h" 13 #import "YYComposeViewController.h" 14 15 @interface YYTabBarViewController ()<UITabBarControllerDelegate,YYTabBarDelegate> 16 17 @end 18 19 @implementation YYTabBarViewController 20 21 22 - (void)viewDidLoad 23 { 24 [super viewDidLoad]; 25 //添加四个子控制器 26 [self addAllChildVcs]; 27 28 // 调整tabbar 29 [self addCustomTabBar]; 30 } 31 32 -(void)addAllChildVcs 33 { 34 YYHomeTableViewController *home=[[YYHomeTableViewController alloc]init]; 35 [self addOneChildVc:home title:@"首页" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_selected"]; 36 37 38 YYMessageViewController *message=[[YYMessageViewController alloc]init]; 39 [self addOneChildVc:message title:@"消息" imageName:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_selected"]; 40 41 YYDiscoverViewController *discover=[[YYDiscoverViewController alloc]init]; 42 [self addOneChildVc:discover title:@"发现" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_selected"]; 43 44 YYProfileViewController *profile=[[YYProfileViewController alloc]init]; 45 [self addOneChildVc:profile title:@"我" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_selected"]; 46 } 47 48 -(void)addCustomTabBar 49 { 50 YYTabBar *customTabBar = [[YYTabBar alloc] init]; 51 //设置代理 52 customTabBar.delegate=self; 53 // 更换系统自带的tabbar 54 [self setValue:customTabBar forKeyPath:@"tabBar"]; 55 } 56 57 58 #pragma mark-YYTabBarDelegate 59 -(void)tabBarDidClickedPlusButton:(YYTabBar *)tabBar 60 { 61 //弹出发微博的控制器 62 YYComposeViewController *compose=[[YYComposeViewController alloc]init]; 63 YYNavigationViewController *nav=[[YYNavigationViewController alloc]initWithRootViewController:compose]; 64 [self presentViewController:nav animated:YES completion:nil]; 65 } 66 67 68 /** 69 * 添加一个子控制器 70 * 71 * @param childVC 子控制对象 72 * @param title 标题 73 * @param imageName 图标 74 * @param selectedImageName 选中时的图标 75 */ 76 -(void)addOneChildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName 77 { 78 //随机设置子控制器的背景颜色 79 // childVc.view.backgroundColor=YYRandomColor; 80 81 //设置标题 82 childVc.title=title; //相当于设置了后两者的标题 83 84 85 //设置图标 86 childVc.tabBarItem.image=[UIImage imageWithName:imageName]; 87 //设置选中时的图标 88 UIImage *selectedImage=[UIImage imageWithName:selectedImageName]; 89 90 //设置tabBarItem普通状态下文字的颜色 91 NSMutableDictionary *textAttrs=[NSMutableDictionary dictionary]; 92 textAttrs[UITextAttributeTextColor]=[UIColor blackColor]; 93 textAttrs[UITextAttributeFont]=[UIFont systemFontOfSize:10]; 94 [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal]; 95 96 //设置tabBarItem普通状态下文字的颜色 97 NSMutableDictionary *selectedtextAttrs=[NSMutableDictionary dictionary]; 98 selectedtextAttrs[UITextAttributeTextColor]=[UIColor orangeColor]; 99 [childVc.tabBarItem setTitleTextAttributes:selectedtextAttrs forState:UIControlStateSelected]; 100 101 if (iOS7) { 102 // 声明这张图片用原图(别渲染) 103 selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 104 } 105 childVc.tabBarItem.selectedImage = selectedImage; 106 107 // 添加为tabbar控制器的子控制器 108 YYNavigationViewController *nav=[[YYNavigationViewController alloc]initWithRootViewController:childVc]; 109 110 [self addChildViewController:nav]; 111 112 } 113 114 @end
4.实现效果
iOS开发项目篇—27自定义UITabBar,布布扣,bubuko.com