這是一個tabBarController(導航控制器)組件。簡單易用,功能強大。輕量、低耦合以及良好的擴展性。通過簡單的幾行代碼可以讓你快速搭建起流行的應用UI架構(gòu)。
普通的TabBar
如果你只想構(gòu)建一個很普通的tabbarController,像這種:

那么你只需要這樣做
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [self rootViewController1];
[self.window makeKeyAndVisible];
return YES;
}
// 設置導航控制器TabBarController
// 傳入一個 `控制器數(shù)組` 和 `選項卡屬性數(shù)組`
- (UIViewController *)rootViewController1
{
NSArray *viewControllers = [self viewControllers];
NSArray *tabBarItems = [self tabBarItems];
DSTabBarController *tabBarController = [DSTabBarController tabBarControllerWithViewControllers:viewControllers tabBarItems:tabBarItems];
return tabBarController;
}
// 這個數(shù)組是 每個選項卡上對應的控制器
// 可以傳字符串、Class或者控制器實例
- (NSArray *)viewControllers
{
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:[[DSFirstTableViewController alloc] initWithStyle:UITableViewStylePlain] ];
return @[nav1,@"DSSecondViewController"];
}
//這是數(shù)組是 每個選項卡的屬性
//數(shù)組中每個元素的類型是 `UITabBarItem`
- (NSArray *)tabBarItems
{
UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"one" image:[UIImage imageNamed:@"my"] selectedImage:[UIImage imageNamed:@"my_h"]];
UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTitle:@"two" image:[UIImage imageNamed:@"home"] selectedImage:[UIImage imageNamed:@"home_h"]];
return @[tabBarItem1,tabBarItem2];
}
DSTabBarController提供了一個遵守<DSTabBarControllerDelegate>的代理ds_delegate。他可以監(jiān)聽選項卡(實際上是UITabbarButton)的按下和長按。
- (UIViewController *)rootViewController1
{
NSArray *viewControllers = [self viewControllers];
NSArray *tabBarItems = [self tabBarItems];
DSTabBarController *tabBarController = [DSTabBarController tabBarControllerWithViewControllers:viewControllers tabBarItems:tabBarItems];
//設置代理
tabBarController.ds_delegate = self;
return tabBarController;
}
實現(xiàn)代理方法
- (void)tabBarController:(DSTabBarController *)tabBarController didClickTabBarButton:(UIControl *)tabBarButton viewController:(UIViewController *)viewController
{
[self addZoonAnimationWithView:tabBarButton];
}
- (void)addZoonAnimationWithView:(UIView *)view
{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"transform.scale";
animation.values = @[@1.0,@1.3,@0.9,@1.15,@0.95,@1.02,@1.0];
animation.duration = 0.9;
animation.calculationMode = kCAAnimationCubic;
[view.layer addAnimation:animation forKey:nil];
}
上面代碼是當選項卡點擊后,給選項卡添加了一個幀動畫。效果如下:

當需要監(jiān)聽選項卡長按時,需要實現(xiàn)下面的代理方法
//tabBarController默認是不能響應長按事件的
//如果需要支持長按 這里返回YES
- (BOOL)tabBarController:(DSTabBarController *)tabBarController shouldLongPressTabBarButton:(UIControl *)tabBarButton viewController:(UIViewController *)viewController
{
return YES;
}
//這個方法里是寫長按要實現(xiàn)的功能
- (void)tabBarController:(DSTabBarController *)tabBarController didLongPressUITabBarButton:(UIControl *)tabBarButton viewController:(UIViewController *)viewController
{
//這里是長按功能的業(yè)務代碼
UIViewController *viewController = tabBarController.publishViewController;
[UIView animateWithDuration:0.25 animations:^{
viewController.view.layer.transform = CATransform3DMakeScale(0.95, 0.95, 0.7);
}];
UIAlertController *alerController = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[UIView animateWithDuration:0.25 animations:^{
viewController.view.layer.transform = CATransform3DIdentity;
}];
}];
[alerController addAction:action];
[viewController presentViewController:alerController animated:YES completion:nil];
}
設置后的效果如下:

你還可以設置長按的時間
@property (nonatomic, assign) CFTimeInterval minimumPressDuration;// Default is 0.8. Time in seconds the fingers must be held down for the gesture to be recognized
特殊的TabBar
像是閑魚客戶端的那種TabBar,TarBar的中間有個特殊的按鈕,大致效果如下:

這就需要數(shù)據(jù)源方法了,首先設置數(shù)據(jù)源
tabBarController.ds_dataSource = self;
實現(xiàn)以下的數(shù)據(jù)源方法
//設置特殊按鈕
- (UIButton *)tabBarControllerPublishButton:(DSTabBarController *)tabBarController
{
return [self publishButton];
}
- (UIButton *)publishButton
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"yuanfenqi"] forState:UIControlStateNormal];[button setImage:[UIImage imageNamed:@"yuanfenqi_h"] forState:UIControlStateSelected];
button.backgroundColor =[UIColor cyanColor];
//默認`sizeToFit`
button.frame = CGRectMake(0, 0, 100, 100);
button.showsTouchWhenHighlighted = YES;
return button;
}
監(jiān)聽特殊按鈕的點擊
- (void)tabBarController:(DSTabBarController *)tabBarController didClickPublishButton:(UIButton *)button
{
//添加關鍵幀動畫
[self addZoonAnimationWithView:button];
//執(zhí)行按鈕點擊后的事件
//這里寫你的業(yè)務邏輯代碼
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor =[UIColor yellowColor];
vc.modalPresentationStyle = UIModalPresentationCustom;
vc.view.alpha = 0.9;
[tabBarController.selectedViewController presentViewController:vc animated:YES completion:nil];
}
關于特殊按鈕的位置
若需要指定特殊按鈕的位置,實現(xiàn)以下數(shù)據(jù)源方法
- (NSUInteger)tabBarControllerPublishButtonIndex:(DSTabBarController *)tabBarController
{
return 1;
}
若沒有指定特殊按鈕的位置,有如下簡單的規(guī)則:
如果TabBar上總選項卡個數(shù)是奇數(shù)個,則特殊按鈕居中;
如果TabBar上總選項卡個數(shù)是偶數(shù)個,特殊按鈕會在最后一個位置。
如果你的應用點擊特殊按鈕不是彈出一個單獨的控制器,而是根TabBar上的其他選項卡一樣,需要實現(xiàn)以下數(shù)據(jù)源方法
- (UIViewController *)tabBarControllerSelectPublishButtonViewController:(DSTabBarController *)tabBarController
{
UIViewController *vc= [[UIViewController alloc] init];
vc.title = @"Text";
vc.view.backgroundColor = [UIColor yellowColor];
return [[UINavigationController alloc] initWithRootViewController:vc];
}
效果如下圖:

監(jiān)聽特殊按鈕的長按
- (BOOL)tabBarController:(DSTabBarController *)tabBarController shouldLongPressPublishButton:(UIButton *)button
{
return YES;
}
//這個方法里是寫長按要實現(xiàn)的功能
- (void)tabBarController:(DSTabBarController *)tabBarController didLongPressPublishButton:(UIButton *)button
{
UIViewController *viewController = tabBarController.publishViewController;
[UIView animateWithDuration:0.25 animations:^{
viewController.view.layer.transform = CATransform3DMakeScale(0.95, 0.95, 0.7);
}];
UIAlertController *alerController = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[UIView animateWithDuration:0.25 animations:^{
viewController.view.layer.transform = CATransform3DIdentity;
}];
}];
[alerController addAction:action];
[viewController presentViewController:alerController animated:YES completion:nil];
}
PS:
最低支持:iOS7
支持Pod:Pod 'DSTabBarController'
Demo地址:DSTabBarController