iOS tabbar中间添加按钮

来源:互联网 发布:量化投资python为工具 编辑:程序博客网 时间:2024/04/28 06:06

如何在系统自带的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]];}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

3、效果图

这里写图片描述

4、Demo 下载

0 0
原创粉丝点击