1. 程式人生 > >iOS tabbar中間新增按鈕

iOS tabbar中間新增按鈕

如何在系統自帶的tabbar上新增一個按鈕?其實實現過程很簡單。比如你的tabbar上本來有四個選項,然後在tabbar中間還新增一個自定義的按鈕,總共就是五個按鈕。實現的思路就是在初始化tabbar的時候,新增五個控制器,然後禁止選擇中間的那個按鈕(第三個按鈕),最後在tabbar上新增你自定義的按鈕。

1、在AppDelegate.m 中新增如下程式碼:

#import "AppDelegate.h"
#import "RootTabbarVC.h"

@interface AppDelegate ()<UITabBarControllerDelegate>

@end
@implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. RootTabbarVC *tabbar = [[RootTabbarVC alloc] init]; tabbar.delegate = self; self
.window.rootViewController = tabbar; self.window.backgroundColor = [UIColor whiteColor]; return YES; } //設定tabbar上第三個按鈕為不可選中狀態,其他的按鈕為可選擇狀態 - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { return ![viewController isEqual:tabBarController.viewControllers
[2]]; }

2、在RootTabbar.m中新增如下程式碼:

#import "RootTabbarVC.h"

@interface RootTabbarVC ()

@property (nonatomic, strong) UIView *baseView;

@end

@implementation RootTabbarVC

- (void)viewDidLoad {
    [super viewDidLoad];

    UINavigationController *nav1 = [self className:@"FirstVC" vcTitle:@"測試一" tabTitle:@"訊息" tabImage:@"tabbar_1" tabSelectedImage:@"tabbar_1a"];
    UINavigationController *nav2 = [self className:@"SecondVC" vcTitle:@"測試二" tabTitle:@"工作" tabImage:@"tabbar_2" tabSelectedImage:@"tabbar_2a"];
    UINavigationController *nav3 = [self className:@"SecondVC" vcTitle:@"" tabTitle:@"" tabImage:@"" tabSelectedImage:@""];
    UINavigationController *nav4 = [self className:@"ThirdVC" vcTitle:@"測試三" tabTitle:@"聯絡人" tabImage:@"tabbar_3" tabSelectedImage:@"tabbar_3a"];
    UINavigationController *nav5 = [self className:@"FourthVC" vcTitle:@"測試四" tabTitle:@"我的" tabImage:@"tabbar_4" tabSelectedImage:@"tabbar_4a"];

    self.viewControllers = @[nav1,nav2,nav3,nav4,nav5];

    //自定義按鈕
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width-40)/2, 5, 40, 40)];
    [btn setImage:[UIImage imageNamed:@"tabbar_add"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(tabBarDidClickPlusButton) forControlEvents:UIControlEventTouchUpInside];
    [self.tabBar addSubview:btn];

}

- (UINavigationController *)className:(NSString *)className
                              vcTitle:(NSString *)vcTitle
                             tabTitle:(NSString *)tabTitle
                             tabImage:(NSString *)image
                     tabSelectedImage:(NSString *)selectedImage
{
    UIViewController *vc = [[NSClassFromString(className) alloc] init];
    vc.title = vcTitle;
    vc.tabBarItem.title = tabTitle;
    vc.tabBarItem.image = [UIImage imageNamed:image];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];

    UINavigationController *navgation = [[UINavigationController alloc] initWithRootViewController:vc];
    return navgation;
}

- (void)tabBarDidClickPlusButton
{
    _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-250, self.view.frame.size.width, 250)];
    _baseView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_baseView];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(romoveBaseView)];
    [self.baseView addGestureRecognizer:tapGesture];
}

- (void)romoveBaseView
{
    [_baseView removeFromSuperview];
}

@end

3、效果圖

這裡寫圖片描述