1. 程式人生 > >iOS 自定義帶加號的tabBar

iOS 自定義帶加號的tabBar

轉載自:http://blog.csdn.net/dh245427794/article/details/73827071

在專案過程中,經常遇到系統原生的UITabBar無法滿足我們的需求,這時候就難免需要我們自己根據需求去自定義一個tabBar了。

原始碼地址

先看一下最後的效果圖:

以下是實現程式碼:

DBHTabBar.h

  1. #import <UIKit/UIKit.h>
  2. @class DBHTabBar;  
  3. @protocol DBHTabBarDelegate <UITabBarDelegate>  
  4. @optional
  5. - (void)tabBarDidClickPlusButton:(
    DBHTabBar *)tabBar;  
  6. @end
  7. @interface DBHTabBar : UITabBar  
  8. @property (nonatomic, weak) id<DBHTabBarDelegate> myDelegate;  
  9. @end

DBHTabBar.m

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

使用的2個步驟方法:

1、在UITabBarController中新建一個tabBar並替換掉原有的tabBar

  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中寫的協議
  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. }