1 #import <UIKit/UIKit.h> 2 @class KKTabBar; 3 4 @protocol KKTabBarDelegate <NSObject> 5 6 - (void)tabBarDidPlusClick:(KKTabBar *)tabBar; 7 8 @end 9 10 @interface KKTabBar : UITabBar 11 12 @property (nonatomic, weak) id<KKTabBarDelegate> tabBarDelegate; 13 14 @end
1 #import "KKTabBar.h" 2 #import "UIView+Extension.h" 3 4 @interface KKTabBar() 5 6 @property (nonatomic, weak) UIButton *plusButton; 7 8 9 @end 10 11 @implementation KKTabBar 12 13 14 15 - (instancetype)initWithFrame:(CGRect)frame{ 16 self = [super initWithFrame:frame]; 17 if (self) { 18 19 // self.backgroundImage = [UIImage imageNamed:@""]; 20 // self.selectionIndicatorImage = [[UIImageb alloc]init]; 21 // self.selectionIndicatorImage = [UIImage imageNamed:@"navigationbar_button_background_os7"]; 22 23 // 添加加号按钮 24 [self setPlusButton]; 25 } 26 return self; 27 } 28 29 // 添加加号按钮 30 - (void)setPlusButton 31 { 32 UIButton *btn = [[UIButton alloc] init]; 33 34 [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal]; 35 [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted]; 36 37 [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal]; 38 [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted]; 39 40 [btn addTarget:self action:@selector(clickPlus:) forControlEvents:UIControlEventTouchUpInside]; 41 42 [self addSubview:btn]; 43 self.plusButton = btn; 44 45 } 46 47 - (void)clickPlus:(UIButton *)sender{ 48 49 NSLog(@"click-- plus"); 50 51 // 通知代理 52 if ([self.tabBarDelegate respondsToSelector:@selector(tabBarDidPlusClick:)]) { 53 [self.tabBarDelegate tabBarDidPlusClick:self]; 54 } 55 56 } 57 58 /** 59 * 布局子控件 60 */ 61 - (void)layoutSubviews 62 { 63 [super layoutSubviews]; 64 65 // 设置plusButton的frame 66 [self setupPlusButtonFrame]; 67 68 // 设置所有tabbarButton的frame 69 [self setupAllTabBarButtonsFrame]; 70 } 71 72 /** 73 * 设置所有plusButton的frame 74 */ 75 - (void)setupPlusButtonFrame 76 { 77 self.plusButton.size = self.plusButton.currentBackgroundImage.size; 78 self.plusButton.center = CGPointMake(self.width * .5, self.height * .5); 79 } 80 81 /** 82 * 设置所有tabbarButton的frame 83 */ 84 - (void)setupAllTabBarButtonsFrame 85 { 86 int index = 0; 87 88 // 遍历所有的button 89 for (UIView *tabBarButton in self.subviews) { 90 // 如果不是UITabBarButton, 直接跳过 91 if (![tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue; 92 93 // 根据索引调整位置 94 [self setupTabBarButtonFrame:tabBarButton atIndex:index]; 95 96 // 索引增加 97 index++; 98 } 99 } 100 101 /** 102 * 设置某个按钮的frame 103 * 104 * @param tabBarButton 需要设置的按钮 105 * @param index 按钮所在的索引 106 */ 107 - (void)setupTabBarButtonFrame:(UIView *)tabBarButton atIndex:(int)index 108 { 109 // 计算button的尺寸 110 CGFloat buttonW = self.width / (self.items.count + 1); 111 CGFloat buttonH = self.height; 112 113 tabBarButton.width = buttonW; 114 tabBarButton.height = buttonH; 115 if (index >= 2) { 116 tabBarButton.x = buttonW * (index + 1); 117 } else { 118 tabBarButton.x = buttonW * index; 119 } 120 tabBarButton.y = 0; 121 } 122 123 @end
使用自定义tabBar
1 #import <UIKit/UIKit.h> 2 3 @interface KKTabBarController : UITabBarController 4 5 @end
1 #import "KKTabBarController.h" 2 #import "OneViewController.h" 3 #import "TwoViewController.h" 4 #import "ThreeViewController.h" 5 #import "FourViewController.h" 6 #import "KKTabBar.h" 7 8 @interface KKTabBarController () 9 10 @end 11 12 @implementation KKTabBarController 13 14 - (instancetype)init 15 { 16 self = [super init]; 17 if (self) { 18 19 20 NSLog(@" KKTabBarController init"); 21 } 22 return self; 23 } 24 25 - (void)viewWillAppear:(BOOL)animated{ 26 [super viewWillAppear:animated]; 27 28 // NSLog(@"%@",self.tabBar.subviews); 29 30 31 } 32 33 - (void)viewDidLoad { 34 [super viewDidLoad]; 35 36 37 KKTabBar *tab = [[KKTabBar alloc] init]; 38 39 // 更换系统默认的tabBar 40 [self setValue:tab forKeyPath:@"tabBar"]; 41 42 OneViewController *one = [[OneViewController alloc] init]; 43 [self addChildVc:one title:@"首页" image:@"tabbar_home" selectedImage:@"tabbar_home_selected_os7"]; 44 45 TwoViewController *two = [[TwoViewController alloc] init]; 46 [self addChildVc:two title:@"信息" image:@"tabbar_message_center_os7" selectedImage:@"tabbar_message_center_selected_os7"]; 47 48 ThreeViewController *three = [[ThreeViewController alloc] init]; 49 [self addChildVc:three title:@"发现" image:@"tabbar_discover_os7" selectedImage:@"tabbar_discover_selected_os7"]; 50 51 FourViewController *four = [[FourViewController alloc] init]; 52 [self addChildVc:four title:@"我" image:@"tabbar_profile_os7" selectedImage:@"tabbar_profile_selected_os7"]; 53 54 55 } 56 57 - (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage{ 58 59 // 放弃系统默认渲染方式 60 UIImage *selectImg = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 61 62 childVc.title = title; 63 childVc.tabBarItem.image = [UIImage imageNamed:image]; 64 childVc.tabBarItem.selectedImage = selectImg; 65 66 // 设置字体默认颜色 67 NSMutableDictionary *dic = [NSMutableDictionary dictionary]; 68 dic[NSForegroundColorAttributeName] = [UIColor blackColor]; 69 [childVc.tabBarItem setTitleTextAttributes:dic forState:UIControlStateNormal]; 70 71 // 设置字体选中颜色 72 NSMutableDictionary *selectDic = [NSMutableDictionary dictionary]; 73 selectDic[NSForegroundColorAttributeName] = [UIColor orangeColor]; 74 [childVc.tabBarItem setTitleTextAttributes:selectDic forState:UIControlStateSelected]; 75 76 [self addChildViewController:childVc]; 77 } 78 79 80 - (void)didReceiveMemoryWarning { 81 [super didReceiveMemoryWarning]; 82 // Dispose of any resources that can be recreated. 83 } 84 85 /* 86 #pragma mark - Navigation 87 88 // In a storyboard-based application, you will often want to do a little preparation before navigation 89 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 90 // Get the new view controller using [segue destinationViewController]. 91 // Pass the selected object to the new view controller. 92 } 93 */ 94 95 @end
原文:http://www.cnblogs.com/kinghx/p/5197096.html