关于tabbar上自定义按钮的设置

首先声明一下,此文档是由本人看到别人写的,然后感觉挺不错的,为了以后查看方便,再次重新写一下,请原作者原谅

1,以前基本上都是用系统的tabbar或者是自己完全自定义的,从来都没有向这两方面合起来想过

首先创建一个project

其次在AppDelegate.m里面这样写道,见


#import "AppDelegate.h"

#import "TabbarVC.h"

@interface AppDelegate ()<UITabBarControllerDelegate>


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    TabbarVC *tabbar = [[TabbarVC alloc]init];

    tabbar.delegate = self;

    

    self.window.backgroundColor = [UIColor whiteColor];

    self.window.rootViewController = tabbar;

    return YES;

}

//设置tabbar上的第三个按钮不可选

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

{

    return  ![viewController isEqual:tabBarController.viewControllers[2]];

}


二,TabbarVC.h 为自定义的tabbar

在tabbar的TabbarVC.m中这样写道

#import "TabbarVC.h"

#import "ViewController.h"

#import "ViewController1.h"

#import "ViewController2.h"

#import "ViewController3.h"

#import "ViewController4.h"

@interface TabbarVC ()<UITabBarControllerDelegate>

@property (nonatomic, strong) UIView *baseView;

@end


@implementation TabbarVC


- (void)viewDidLoad {

    [super viewDidLoad];

    UINavigationController *vC = [self className:@"ViewController" vcTitle:@"消息" tabTitle:@"消息" tabImage:@"release" tabSelectedImage:@"release"];

    

    UINavigationController *vC1 = [self className:@"ViewController1" vcTitle:@"通知" tabTitle:@"通知" tabImage:@"U-assn" tabSelectedImage:@"U-assn"];

    

    UINavigationController *vC2 = [self className:@"ViewController2" vcTitle:@"人物" tabTitle:@"人物" tabImage:@"U-city" tabSelectedImage:@"U-city"];

    

    UINavigationController *vC3 = [self className:@"ViewController3" vcTitle:@"列表" tabTitle:@"列表" tabImage:@"U-me" tabSelectedImage:@"U-me"];

    

    UINavigationController *vC4 = [self className:@"ViewController4" vcTitle:@"你好" tabTitle:@"你好" tabImage:@"UU" tabSelectedImage:@"UU"];

    self.viewControllers = @[vC,vC1,vC2,vC3,vC4];

    

    //自定义按钮

    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake((self.view.bounds.size.width - 40)/2, 5, 40, 40)];

    [btn setImage:[UIImage imageNamed:@"point"] 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, 200)];

    _baseView.backgroundColor = [UIColor orangeColor];

    [self.view addSubview:_baseView];

    

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

    [_baseView addGestureRecognizer:tapGesture];

}


- (void)romoveBaseView

{

    [_baseView removeFromSuperview];

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值