iOS 自定义带加号的tabBar

转载自:http://blog.csdn.net/dh245427794/article/details/73827071


在项目过程中,经常遇到系统原生的UITabBar无法满足我们的需求,这时候就难免需要我们自己根据需求去自定义一个tabBar了。

源码地址

先看一下最后的效果图:

以下是实现代码:

DBHTabBar.h

[objc]  view plain  copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @class DBHTabBar;  
  4.   
  5. @protocol DBHTabBarDelegate <UITabBarDelegate>  
  6.   
  7. @optional  
  8.   
  9. - (void)tabBarDidClickPlusButton:(DBHTabBar *)tabBar;  
  10.   
  11. @end  
  12.   
  13. @interface DBHTabBar : UITabBar  
  14.   
  15. @property (nonatomic, weak) id<DBHTabBarDelegate> myDelegate;  
  16.   
  17. @end  

DBHTabBar.m

[objc]  view plain  copy
  1. #import "DBHTabBar.h"  
  2.   
  3. @interface DBHTabBar ()  
  4.   
  5. @property (nonatomicstrongUIButton *plusButton;  
  6.   
  7. @end  
  8.   
  9. @implementation DBHTabBar  
  10.   
  11. #pragma mark - Lifecycle  
  12. - (instancetype)initWithFrame:(CGRect)frame  
  13. {  
  14.     self = [super initWithFrame:frame];  
  15.     if (self) {  
  16.         [self addSubview:self.plusButton];  
  17.     }  
  18.     return self;  
  19. }  
  20.   
  21. #pragma mark - Event Responds  
  22. /** 
  23.  * 点击了加号按钮 
  24.  */  
  25. - (void)respondsToPlusButton  
  26. {  
  27.     // 通知代理  
  28.     if ([self.delegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {  
  29.         [self.myDelegate tabBarDidClickPlusButton:self];  
  30.     }  
  31. }  
  32.   
  33. #pragma mark - Private Methods  
  34. /** 
  35.  *  重新布局系统tabBarItem 
  36.  */  
  37. - (void)layoutSubviews  
  38. {  
  39.     [super layoutSubviews];  
  40.       
  41.     // 1.设置加号按钮的位置  
  42.     self.plusButton.center = CGPointMake(CGRectGetWidth(self.frame) * 0.5, CGRectGetHeight(self.frame) * 0.1);  
  43.       
  44.     // 2.设置其他tabbarButton的frame  
  45.     CGFloat tabBarButtonWidth = CGRectGetWidth(self.frame) / 5;  
  46.     CGFloat tabBarButtonIndex = 0;  
  47.     for (UIView *childView in self.subviews) {  
  48.         Class class = NSClassFromString(@"UITabBarButton");  
  49.         if ([childView isKindOfClass:class]) {  
  50.             // 设置位置  
  51.             childView.frame = CGRectMake(tabBarButtonWidth * tabBarButtonIndex, CGRectGetMinY(childView.frame), tabBarButtonWidth, CGRectGetHeight(childView.frame));  
  52.             // 增加索引  
  53.             tabBarButtonIndex += (tabBarButtonIndex == 1 ? 2 : 1);  
  54.         }  
  55.     }  
  56. }  
  57. /** 
  58.  重写hitTest方法以响应点击超出tabBar的加号按钮 
  59.  */  
  60. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {  
  61.     if (!self.clipsToBounds && !self.hidden && self.alpha > 0) {  
  62.         UIView *result = [super hitTest:point withEvent:event];  
  63.         if (result) {  
  64.             return result;  
  65.         }  
  66.         else {  
  67.             for (UIView *subview in self.subviews.reverseObjectEnumerator) {  
  68.                 CGPoint subPoint = [subview convertPoint:point fromView:self];  
  69.                 result = [subview hitTest:subPoint withEvent:event];  
  70.                 if (result) {  
  71.                     return result;  
  72.                 }  
  73.             }  
  74.         }  
  75.     }  
  76.     return nil;  
  77. }  
  78.   
  79. #pragma mark - Getters And Setters  
  80. - (UIButton *)plusButton {  
  81.     if (!_plusButton) {  
  82.         _plusButton = [[UIButton alloc] init];  
  83.         [_plusButton setImage:[UIImage imageNamed:@"plusButton"] forState:UIControlStateNormal];  
  84.         [_plusButton setImage:[UIImage imageNamed:@"plusButton_selected"] forState:UIControlStateHighlighted];  
  85.           
  86.         _plusButton.frame = CGRectMake(00, _plusButton.imageView.image.size.width, _plusButton.imageView.image.size.height);  
  87.         [_plusButton addTarget:self action:@selector(respondsToPlusButton) forControlEvents:UIControlEventTouchUpInside];  
  88.     }  
  89.     return _plusButton;  
  90. }  
  91.   
  92. @end  

使用的2个步骤方法:

1、在UITabBarController中新建一个tabBar并替换掉原有的tabBar

[objc]  view plain  copy
  1. DBHTabBar *tabBar = [[DBHTabBar alloc] init];  
  2.     //取消tabBar的透明效果  
  3.     tabBar.translucent = NO;  
  4.     // 设置tabBar的代理  
  5.     tabBar.myDelegate = self;  
  6.     // KVC:如果要修系统的某些属性,但被设为readOnly,就是用KVC,即setValue:forKey:。  
  7.     [self setValue:tabBar forKey:@"tabBar"];  
2、实现刚才自定义的tabBar中写的协议

[objc]  view plain  copy
  1. /** 
  2.  *  点击了加号按钮 
  3.  */  
  4. - (void)tabBarDidClickPlusButton:(DBHTabBar *)tabBar  
  5. {  
  6.     // 在这里写你点击加号按钮需要做的操作,我这里是弹出一个提示框  
  7.     UIAlertController *promptAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"点击了加号按钮" preferredStyle:UIAlertControllerStyleAlert];  
  8.     [promptAlert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];  
  9.     [self presentViewController:promptAlert animated:YES completion:nil];  
  10. }  

友情提示:当加号按钮超出tabBar时,点击超出的那一部分按钮事件将不会响应。快哭了不过,只需要重写tabBar中hitTest方法即可解决哦大笑,重写方式已经在代码中贴出来了哦。

猜你喜欢

转载自blog.csdn.net/iotjin/article/details/79563642