首先声明一下,此文档是由本人看到别人写的,然后感觉挺不错的,为了以后查看方便,再次重新写一下,请原作者原谅
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];
}