自定义UITabbarController(含突出按钮)

1.思路

       最近项目中需要在tabbar中间做一个突出按钮,查了很多资料也没看到,于是按照自己的想法写了个,基本算是实现了。具体思路是给tabbbar中间添加多一个item ,然后在中间添加一个按钮就行了,把按钮暴露出来,然后做跳转的时候直接隐藏按钮。

2.自定义步骤

  新建一个类继承UITabBarController,再把每个包含UIViewController的 UINavigationController添加到item。可能你觉得说的不清楚,看代码会明白些:

.h文件 

@interface MyTabBarController : UITabBarController

@property(nonatomic,strong)UIButton *centerButton;

@property(nonatomic,strong)UILabel *centerLabel;

@end


.m文件部分重点代码

#pragma mark-设置导航栏和添加控制器

-(void)setVC{

self.tabBar.tintColor = [UIColor whiteColor];

// 背景更改为白色

UIView *view = [[UIView alloc] init];

view.backgroundColor = [UIColor whiteColor];

view.frame = self.tabBar.bounds;

[self.tabBar insertSubview:view atIndex:0];

//这里是每个控制器

NearByViewController *nearbyVC = [[NearByViewController alloc] init];

[self.navigationController pushViewController:nearbyVC animated:YES];

[self setupChildViewController:nearbyVC title:@"附近" image:@"nearby_unselect" selectedImage:@"nearby"];

DynamicViewController *dynamicVC = [[DynamicViewController alloc]init];

[self.navigationController pushViewController:dynamicVC animated:YES];

[self setupChildViewController:dynamicVC title:@"动态" image:@"dynamic_unselect" selectedImage:@"dynamic"];

UIViewController *vc = [[UIViewController alloc]init];

[self setupChildViewController:vc title:@"" image:@"" selectedImage:@""];

MessageViewController *messageVC = [[MessageViewController alloc]init];

[self setupChildViewController:messageVC title:@"消息" image:@"news_unselect" selectedImage:@"news"];

MineViewController *mineVC = [[MineViewController alloc]init];

[self setupChildViewController:mineVC title:@"我的" image:@"mine_unselect" selectedImage:@"mine"];

[self addMiddleBtn];

self.delegate = self;

//设置阴影

self.tabBar.layer.shadowColor = RGB(159, 159, 159).CGColor;//shadowColor阴影颜色

self.tabBar.layer.shadowOffset = CGSizeMake(2.0f,5.0f);//shadowOffset阴影偏移x,y向(上/下)偏移(-/+)2

self.tabBar.layer.shadowOpacity = 0.8f;//阴影透明度,默认0

self.tabBar.layer.shadowRadius = 6.0f;//阴影半径

//去掉黑线

self.tabBar.barStyle = UIBarStyleBlack;

}

//当控制器为中间的控制器时,返回no

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

if(viewController == [tabBarController.viewControllers objectAtIndex:2]){

return NO;

}else {

return YES;

}

}


#pragma mark-添加中间突出按钮 和标题

-(void)addMiddleBtn{

//创建一个自定义button

_centerButton = [UIButton buttonWithType:UIButtonTypeCustom];

//初始化button的背景图片

UIImage *centerButtonImg = [UIImage imageNamed:@"pp"];

//设置button的frame

_centerButton.frame = CGRectMake(0, 0, centerButtonImg.size.width, centerButtonImg.size.height);

//设置button的背景图片

[_centerButton setBackgroundImage:centerButtonImg forState:UIControlStateNormal];

CGFloat imageHeight = centerButtonImg.size.height;

//NSLog(@"imageHeight=%2.f,barHeight=%.2f",imageHeight,barHeight);

CGFloat delta = imageHeight/3.0+1;

CGPoint center = self.tabBar.center;

center.y = center.y - delta;

_centerButton.center = center;

//点击btn

[_centerButton addTarget:self action:@selector(issueBgViewShow) forControlEvents:UIControlEventTouchUpInside];

//将centerButton加入到tabBarController中

[self.view addSubview:_centerButton];

_centerLabel = [[UILabel alloc]init];

_centerLabel.text = @"发现";

_centerLabel.textAlignment = NSTextAlignmentCenter;

_centerLabel.textColor = [UIColor grayColor];

_centerLabel.font = [UIFont systemFontOfSize:10];

[self.view addSubview:_centerLabel];

[_centerLabel mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.mas_equalTo(_centerButton.mas_bottom).offset(3);

make.centerX.mas_equalTo(_centerButton);

make.height.mas_equalTo(@15);

}];

}


/**

* 初始化子控制器

*/

-(void)setupChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString*) selectedImage{

//    vc.navigationItem.title = title;

//    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : RGB(255, 0,79)} forState:UIControlStateNormal];

[vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : RGB(255, 0,79)} forState:UIControlStateSelected];

vc.tabBarItem.title = title;

vc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

//    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];

MyNavViewController *nav = [[MyNavViewController alloc]initWithRootViewController:vc];

[self addChildViewController:nav];

}

/**

*  点击tabbar触发的方法

*/

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {

NSInteger index = [self.tabBar.items indexOfObject:item];

}


注意:因为中间按钮是不包含导航栏控制器的,所以如果点击中间按钮跳转到其他页面,只能用模态跳转,写的文字比较少,有什么不懂的评论,反正我也不一定知道。最后贴上效果图


你可能感兴趣的:(自定义UITabbarController(含突出按钮))