iOS 自定義帶加號的tabBar
阿新 • • 發佈:2019-01-04
轉載自:http://blog.csdn.net/dh245427794/article/details/73827071
在專案過程中,經常遇到系統原生的UITabBar無法滿足我們的需求,這時候就難免需要我們自己根據需求去自定義一個tabBar了。
先看一下最後的效果圖:
以下是實現程式碼:
DBHTabBar.h:
- #import <UIKit/UIKit.h>
- @class DBHTabBar;
- @protocol DBHTabBarDelegate <UITabBarDelegate>
- @optional
-
- (void)tabBarDidClickPlusButton:(
- @end
- @interface DBHTabBar : UITabBar
- @property (nonatomic, weak) id<DBHTabBarDelegate> myDelegate;
- @end
DBHTabBar.m
- #import "DBHTabBar.h"
- @interface DBHTabBar ()
- @property (nonatomic, strong) UIButton *plusButton;
- @end
- @implementation DBHTabBar
-
#pragma mark - Lifecycle
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self addSubview:self.plusButton];
- }
- returnself;
- }
- #pragma mark - Event Responds
- /**
- * 點選了加號按鈕
- */
- - (void)respondsToPlusButton
- {
- // 通知代理
-
if ([self.delegate
- [self.myDelegate tabBarDidClickPlusButton:self];
- }
- }
- #pragma mark - Private Methods
- /**
- * 重新佈局系統tabBarItem
- */
- - (void)layoutSubviews
- {
- [super layoutSubviews];
- // 1.設定加號按鈕的位置
- self.plusButton.center = CGPointMake(CGRectGetWidth(self.frame) * 0.5, CGRectGetHeight(self.frame) * 0.1);
- // 2.設定其他tabbarButton的frame
- CGFloat tabBarButtonWidth = CGRectGetWidth(self.frame) / 5;
- CGFloat tabBarButtonIndex = 0;
- for (UIView *childView in self.subviews) {
- Class class = NSClassFromString(@"UITabBarButton");
- if ([childView isKindOfClass:class]) {
- // 設定位置
- childView.frame = CGRectMake(tabBarButtonWidth * tabBarButtonIndex, CGRectGetMinY(childView.frame), tabBarButtonWidth, CGRectGetHeight(childView.frame));
- // 增加索引
- tabBarButtonIndex += (tabBarButtonIndex == 1 ? 2 : 1);
- }
- }
- }
- /**
- 重寫hitTest方法以響應點選超出tabBar的加號按鈕
- */
- - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
- if (!self.clipsToBounds && !self.hidden && self.alpha > 0) {
- UIView *result = [super hitTest:point withEvent:event];
- if (result) {
- return result;
- }
- else {
- for (UIView *subview in self.subviews.reverseObjectEnumerator) {
- CGPoint subPoint = [subview convertPoint:point fromView:self];
- result = [subview hitTest:subPoint withEvent:event];
- if (result) {
- return result;
- }
- }
- }
- }
- return nil;
- }
- #pragma mark - Getters And Setters
- - (UIButton *)plusButton {
- if (!_plusButton) {
- _plusButton = [[UIButton alloc] init];
- [_plusButton setImage:[UIImage imageNamed:@"plusButton"] forState:UIControlStateNormal];
- [_plusButton setImage:[UIImage imageNamed:@"plusButton_selected"] forState:UIControlStateHighlighted];
- _plusButton.frame = CGRectMake(0, 0, _plusButton.imageView.image.size.width, _plusButton.imageView.image.size.height);
- [_plusButton addTarget:self action:@selector(respondsToPlusButton) forControlEvents:UIControlEventTouchUpInside];
- }
- return _plusButton;
- }
- @end
使用的2個步驟方法:
1、在UITabBarController中新建一個tabBar並替換掉原有的tabBar
- DBHTabBar *tabBar = [[DBHTabBar alloc] init];
- //取消tabBar的透明效果
- tabBar.translucent = NO;
- // 設定tabBar的代理
- tabBar.myDelegate = self;
- // KVC:如果要修系統的某些屬性,但被設為readOnly,就是用KVC,即setValue:forKey:。
- [self setValue:tabBar forKey:@"tabBar"];
- /**
- * 點選了加號按鈕
- */
- - (void)tabBarDidClickPlusButton:(DBHTabBar *)tabBar
- {
- // 在這裡寫你點選加號按鈕需要做的操作,我這裡是彈出一個提示框
- UIAlertController *promptAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"點選了加號按鈕" preferredStyle:UIAlertControllerStyleAlert];
- [promptAlert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil]];
- [self presentViewController:promptAlert animated:YES completion:nil];
- }