您好,登錄后才能下訂單哦!
自定義UITabBarController子類
UITabBarController的tabBar往往不能滿足我們的需求
通過自定義UITabBarController子類自定義標簽欄是經常采用的方式。
基本步驟:
1)定義UIButton子類作為標簽按鈕
2)定義模型類,管理每個頁面的控制器以及對應標簽欄上的數據
3)自定義標簽欄
4)定義UITabBarController子類
定義標簽按鈕
添加兩種創建方法:只顯示圖片和文字圖片都顯示的
+ (AMTabBarButton *)tabBarButtonWithTitle:(NSString *)title normalImage:(UIImage *)normalImage selectedImage:(UIImage *)selectedImage { AMTabBarButton * btn = [AMTabBarButton buttonWithType:UIButtonTypeCustom]; [btn setImage:normalImage forState:UIControlStateNormal]; [btn setImage:selectedImage forState:UIControlStateSelected]; [btn setTitle:title forState:UIControlStateNormal]; [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected]; btn.titleLabel.font = [UIFont systemFontOfSize:10]; btn.titleLabel.textAlignment = NSTextAlignmentCenter; return btn; } + (AMTabBarButton *)tabBarButtonWithNormalImage:(UIImage *)normalImage selectedImage:(UIImage *)selectedImage { AMTabBarButton * btn = [AMTabBarButton buttonWithType:UIButtonTypeCustom]; [btn setImage:normalImage forState:UIControlStateNormal]; [btn setImage:selectedImage forState:UIControlStateSelected]; [btn setTitle:nil forState:UIControlStateNormal]; return btn; }
修改按鈕內部label和p_w_picpathView的位置,重寫以下方法即可
- (CGRect)titleRectForContentRect:(CGRect)contentRect - (CGRect)p_w_picpathRectForContentRect:(CGRect)contentRect
如:
- (CGRect)p_w_picpathRectForContentRect:(CGRect)contentRect { CGFloat x, y, w, h; if ( [self titleForState:UIControlStateNormal] == nil ) { w = h = contentRect.size.height*0.8; } else { w = h = contentRect.size.height*0.45; } x = contentRect.size.width/2 - w/2; y = contentRect.size.height*0.1; return CGRectMake(x, y, w, h); } - (CGRect)titleRectForContentRect:(CGRect)contentRect { CGFloat x, y, w, h; if ( [self titleForState:UIControlStateNormal] == nil ) { return CGRectZero; } h = contentRect.size.height*0.25; w = contentRect.size.width*0.6; x = contentRect.size.width*0.2; y = contentRect.size.height*0.65; return CGRectMake(x, y, w, h); }
取消按鈕點擊時的高亮效果:重寫highlighted屬性的setter方法
- (void)setHighlighted:(BOOL)highlighted { }
定義模型類
管理每個子控制器及標簽欄上按鈕的數據
@interface AMTabBarItemModel : NSObject @property (nonatomic, copy) UIImage * normalImage; @property (nonatomic, copy) UIImage * selectedImage; @property (nonatomic, copy) NSString * title; @property (nonatomic, strong) UIViewController * viewController; + (instancetype) tabBarItemModelWithNormalImage:(UIImage*) normalImage selectedImage:(UIImage*) selectedImage title:(NSString*) title viewController:(UIViewController*) viewController; @end
定義標簽欄
定義一個UIView的子類作為自定義的標簽類
設置樣式屬性,兩種樣式:只有圖片和文字圖片都有的
typedef enum { AMTabBarStyleTitleAndImage, AMTabBarStyleImageOnly }AMTabBarStyle;
創建方法:
+ (instancetype)tabBarViewWithItemModels:(NSArray *)models tabBarStyle:(AMTabBarStyle)tabBarStyle { return [[self alloc] initWithItemModels:models tabBarStyle:tabBarStyle]; } - (instancetype) initWithItemModels:(NSArray*) models tabBarStyle:(AMTabBarStyle) tabBarStyle { if ( self = [super init] ) { self.tabBarStyle = tabBarStyle; int i = 1; for ( AMTabBarItemModel * model in models ) { AMTabBarButton * btn; if ( tabBarStyle == AMTabBarStyleTitleAndImage ) { btn = [AMTabBarButton tabBarButtonWithTitle:model.title normalImage:model.normalImage selectedImage:model.selectedImage]; } else { btn = [AMTabBarButton tabBarButtonWithNormalImage:model.normalImage selectedImage:model.selectedImage]; } [self addSubview:btn]; [btn addTarget:self action:@selector(itemBtnClicked:) forControlEvents:UIControlEventTouchDown]; btn.tag = i; i++; } self.selectedBtn = self.subviews[0]; } return self; } - (void)layoutSubviews { [super layoutSubviews]; CGFloat x, y, w, h; h = self.frame.size.height; w = self.frame.size.width/self.subviews.count; y = x = 0; for ( AMTabBarButton * btn in self.subviews ) { btn.frame = CGRectMake(x, y, w, h); x += w; } }
參數items:模型對象數組
當標簽按鈕被點擊時發送代理方法:
- (void) itemBtnClicked:(AMTabBarButton*) sender { self.selectedBtn = sender; if ( self.delegate && [self.delegate respondsToSelector:@selector(tabBarView:selectedIndex:)]) { [self.delegate tabBarView:self selectedIndex:sender.tag]; } } - (void) setSelectedBtn:(AMTabBarButton *)selectedBtn { if ( _selectedBtn != selectedBtn ) { if ( _selectedBtn != nil ) { _selectedBtn.selected = NO; } _selectedBtn = selectedBtn; _selectedBtn.selected = YES; } }
其中selectBtn屬性表示當前被選擇的按鈕
定義UITabBarController子類
添加屬性:樣式、標簽欄、模型數組
@property (nonatomic, assign) AMTabBarStyle tabBarStyle; @property (nonatomic, weak) AMTabBarView * myTabBar; @property (nonatomic, strong) NSArray * items;
添加創建控制器子類的方法:
+ (instancetype)tabBarControllerWithItems:(NSArray *)items tabBarStyle:(AMTabBarStyle)tabBarStyle { AMTabBarController * tabBarController = [[UIStoryboard storyboardWithName:@"AMTabBarController" bundle:nil] instantiateInitialViewController]; tabBarController.items = items; tabBarController.tabBarStyle = tabBarStyle; NSMutableArray * arr = [NSMutableArray array]; for ( AMTabBarItemModel * model in items ) { [arr addObject:model.viewController]; model.viewController = nil; } tabBarController.viewControllers = [arr copy]; return tabBarController; }
創建自定義的標簽欄,添加到tabBar視圖上
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self setupTabBar]; } - (void) setupTabBar { AMTabBarView * myTabBar = [AMTabBarView tabBarViewWithItemModels:self.items tabBarStyle:self.tabBarStyle]; self.myTabBar = myTabBar; [self.tabBar addSubview:self.myTabBar]; self.myTabBar.delegate = self; } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; self.myTabBar.frame = self.tabBar.bounds; }
實現自定義標簽欄的代理方法,切換頁面
- (void)tabBarView:(AMTabBarView *)tabBarView selectedIndex:(NSInteger)index { self.selectedIndex = index-1; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。