生活随笔
收集整理的這篇文章主要介紹了
iOS之自定义封装tabBar
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
- 首先需要創(chuàng)建一個自定義的tabBar的類,繼承于UIview:實現(xiàn)自定義創(chuàng)建tabBar的item按鈕,自定義tabBarDelegate,回調(diào)block等,具體代碼如下:
// YDWTabBar.h
// YDWLiveShow
//
// Created by cptech on 2017/9/6.
// Copyright ? 2017年 CPTECH_ydw. All rights reserved.
//#import <UIKit/UIKit.h>
@class YDWTabBar;// item枚舉
typedef NS_ENUM(NSInteger, itemType) {itemTypeLive = 100, // 直播界面itemTypeMe, // 個人中心itemTypeLanuch = 10 // 啟動直播
};// tabBar協(xié)議
@protocol YDWTabBarDelegate <NSObject>- (void)tabBar:(YDWTabBar *)tabBar selectItem:(NSInteger)idx;@end// tabBar回調(diào)block
typedef void(^tabBarBlock)(YDWTabBar *tabBar, itemType idx);@interface YDWTabBar : UIView@property (nonatomic, weak) id<YDWTabBarDelegate> delegate;
@property (nonatomic, copy) tabBarBlock block;@end// YDWTabBar.m
// YDWLiveShow
//
// Created by cptech on 2017/9/6.
// Copyright ? 2017年 CPTECH_ydw. All rights reserved.
//#import "YDWTabBar.h"@interface YDWTabBar ()@property (nonatomic, strong) UIImageView *tabBarBgView; // tabBar背景@property (nonatomic, copy) NSArray *itemsArray; // item圖片數(shù)組@property (nonatomic, strong) UIButton *selectItem; // tabBar按鈕
@property (nonatomic, strong) UIButton *liveButton; // 直播按鈕@end@implementation YDWTabBar- (void)layoutSubviews {[super layoutSubviews];CGFloat width = self.bounds.size.width/self.itemsArray.count;for (int i = 0; i < [self subviews].count; i++) {UIView *view = [self subviews][i];if ([view isKindOfClass:[UIButton class]]) {view.frame = CGRectMake((view.tag-itemTypeLive)*width, 0, width, self.frame.size.height);}}[self.liveButton sizeToFit];self.liveButton.center = CGPointMake(self.center.x, self.bounds.size.height-50);[self bringSubviewToFront:self.liveButton];
}- (instancetype)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];if (self) {[self addSubview:self.tabBarBgView];[self addItemButton];[self addSubview:self.liveButton];}return self;
}- (void)addItemButton {for (int i = 0; i < self.itemsArray.count; i++) {UIButton *itemButton = [UIButton buttonWithType:UIButtonTypeCustom];itemButton.tag = itemTypeLive+i;[itemButton setTitle:self.itemsArray[i][@"title"] forState:UIControlStateNormal];[itemButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];[itemButton setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];[itemButton.titleLabel setFont:[UIFont systemFontOfSize:11]];[itemButton.titleLabel setTextAlignment:NSTextAlignmentCenter];itemButton.adjustsImageWhenHighlighted = NO; // 禁止圖片在高亮狀態(tài)下改變[itemButton setImage:[UIImage imageNamed:self.itemsArray[i][@"image"]] forState:UIControlStateNormal];[itemButton setImage:[UIImage imageNamed:[self.itemsArray[i][@"image"] stringByAppendingString:@"_p"]] forState:UIControlStateSelected];[itemButton addTarget:self action:@selector(responderToItemSelected:) forControlEvents:UIControlEventTouchUpInside];CGSize imageSize = itemButton.imageView.frame.size;CGSize titleSize = itemButton.titleLabel.frame.size;itemButton.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, imageSize.height-37, 20);itemButton.imageEdgeInsets = UIEdgeInsetsMake(-titleSize.height-5, 0, 0, -titleSize.width);[self addSubview:itemButton];if (i == 0) {itemButton.selected = YES;self.selectItem = itemButton;}}
}#pragma mark - Events Responder
- (void)responderToItemSelected:(UIButton *)sender {// 響應tabBar協(xié)議的方法就回調(diào)if ([self.delegate respondsToSelector:@selector(tabBar:selectItem:)]) {[self.delegate tabBar:self selectItem:sender.tag];}// !self.block?:self.block(self, sender.tag);if (self.block) {self.block(self, sender.tag);}if (sender.tag == itemTypeLanuch) {return;}self.selectItem.selected = NO;sender.selected = YES;self.selectItem = sender;// item添加動畫[UIView animateWithDuration:0.2 animations:^{sender.transform = CGAffineTransformMakeScale(1.2, 1.2);} completion:^(BOOL finished) {[UIView animateWithDuration:0.2 animations:^{sender.transform = CGAffineTransformIdentity;}];}];
}#pragma mark - Setter/Getter
- (UIImageView *)tabBarBgView {if (!_tabBarBgView) {_tabBarBgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"global_tab_bg"]];}return _tabBarBgView;
}- (NSArray *)itemsArray {if (!_itemsArray) {_itemsArray = @[@{@"image":@"tab_live",@"title":@"直播"},@{@"image":@"tab_me", @"title":@"我的"}];}return _itemsArray;
}- (UIButton *)liveButton {if (!_liveButton) {_liveButton = [UIButton buttonWithType:UIButtonTypeCustom];[_liveButton setImage:[UIImage imageNamed:@"tab_launch"] forState:UIControlStateNormal];_liveButton.tag = itemTypeLanuch;[_liveButton addTarget:self action:@selector(responderToItemSelected:) forControlEvents:UIControlEventTouchUpInside];}return _liveButton;
}@end
- 其次,創(chuàng)一個tabBar的控制器,繼承于UITabBarController:主要是裝載生成自定義的tabBar,并配置界面視圖控制器,代碼如下:(當然,這其中,需要創(chuàng)建切換tabBar而展示的不同ViewControlles,我創(chuàng)建了“YDWLiveShowViewController”和“YDWMeCenterViewController”兩個視圖控制器界面,最好是繼承于一個BaseViewController,方便以后修改導航欄和tabBar,還需要創(chuàng)建一個YDWNavViewController,繼承于UINavigationController)
// YDWTabBarViewController.h
// YDWLiveShow
//
// Created by cptech on 2017/9/6.
// Copyright ? 2017年 CPTECH_ydw. All rights reserved.
//#import <UIKit/UIKit.h>@interface YDWTabBarViewController : UITabBarController@end// YDWTabBarViewController.m
// YDWLiveShow
//
// Created by cptech on 2017/9/6.
// Copyright ? 2017年 CPTECH_ydw. All rights reserved.
//#import "YDWTabBarViewController.h"
#import "YDWTabBar.h"
#import "YDWNavViewController.h"
#import "YDWLaunchLiveViewController.h"@interface YDWTabBarViewController ()<YDWTabBarDelegate>@property (nonatomic, strong) YDWTabBar *ydwTabBar; // 自定義的tabBar@end@implementation YDWTabBarViewController- (void)viewDidLoad {[super viewDidLoad];[self configViewControllers];[self initilizeFace];
}#pragma mark - Private Methods
- (void)configViewControllers {NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"YDWLiveShowViewController",@"YDWMeCenterViewController"]];for (int i = 0; i < array.count; i++) {UIViewController *vc = [[NSClassFromString(array[i]) alloc] init];YDWNavViewController *nav = [[YDWNavViewController alloc] initWithRootViewController:vc];[array replaceObjectAtIndex:i withObject:nav];}self.viewControllers = array;
}- (void)initilizeFace {[self.tabBar addSubview:self.ydwTabBar];// 去除tabBar的陰影線[[UITabBar appearance] setShadowImage:[UIImage new]];[[UITabBar appearance] setBackgroundImage:[UIImage new]];
}#pragma mark - YDWTabBarDelegate
- (void)tabBar:(YDWTabBar *)tabBar selectItem:(NSInteger)idx {if (idx != itemTypeLanuch) {self.selectedIndex = idx - itemTypeLive;return;}YDWLaunchLiveViewController *launVC = [[YDWLaunchLiveViewController alloc] init];[self presentViewController:launVC animated:YES completion:nil];
}#pragma mark - Setter/Getter
- (YDWTabBar *)ydwTabBar {if (!_ydwTabBar) {_ydwTabBar = [[YDWTabBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 49)];_ydwTabBar.delegate = self;}return _ydwTabBar;
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}@end
- 最后在AppDelegate中初始化tabBarController即可:
YDWTabBarViewController *vc = [[YDWTabBarViewController alloc] init];
self.window.rootViewController = vc;
總結
以上是生活随笔為你收集整理的iOS之自定义封装tabBar的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。