-1. 为什么要自定义呢?
大多数的App都有用到自定义,因为往往系统提供给我们的,无法满足设计的需求,和我们想要的效果,那么,我们就该想到自定义来实现。比如最常见的,QQ的TabBar很明显就是通过自定义实现的。
- 2 实现自定义的TabBar
首先我们新建一个 **TabBarViewController : UITabBarController
既然是自定义的,那么在TabBar的显示和隐藏,是否可以自定义呢。答案,当然是可以的。
/**
* 自定义的TabBar:
*/
@interface SXTabBarViewController : UITabBarController
/**
* 背景视图:
*/
@property (nonatomic,strong) UIView * BackgroundView;
/**
* 背景图片
*/
@property (nonatomic,strong) UIImageView * imageView;
/**
* 显示或隐藏TabBar
*
* @param isHideen 是否隐藏
* @param animated 是否需要动画
*/
- (void)HideTabarView:(BOOL)isHideen animated:(BOOL)animated;
- 3 加载控制器
-1.首先,创建我们的功能模块控制器,也就是常用的。实现隐藏和显示TabBar的
方法。在这个方法里面我们可以自定义多种动画,有兴趣都可以去研究。比如,一些我们想要的转场动画。
OneViewController * oneVC = [[OneViewController alloc]init];
oneVC.title = @"测试一";
TwoViewController * twoVC = [[TwoViewController alloc]init];
twoVC.title = @"测试二";
ThreeViewController * three = [[ThreeViewController alloc]init];
three.title = @"测试三";
UINavigationController * nav1 = [[UINavigationController alloc]initWithRootViewController:oneVC];
[nav1.navigationBar setBarTintColor:[UIColor cyanColor]];
UINavigationController * nav2 = [[UINavigationController alloc]initWithRootViewController:twoVC];
[nav2.navigationBar setBarTintColor:[UIColor yellowColor]];
UINavigationController * nav3 = [[UINavigationController alloc]initWithRootViewController:three];
[nav3.navigationBar setBarTintColor:[UIColor redColor]];
//隐藏系统的
self.tabBar.hidden = YES;
//添加控制器
self.viewControllers = @[nav1,nav2,nav3];
-2.隐藏或显示tarBar的方法
#pragma mark - == 显示或隐藏TabBar ===
//隐藏
- (void)HideTabarView:(BOOL)isHideen animated:(BOOL)animated;
{
//隐藏
if (isHideen == YES)
{
//需要动画
if (animated)
{
[UIView animateWithDuration:0.6 animations:^{
//旋转动画:
_BackgroundView.transform = CGAffineTransformRotate(_BackgroundView.transform, M_PI);
_BackgroundView.alpha = 0;
}];
}
//没有动画
else
{
_BackgroundView.alpha = 0;
}
}
//显示
else
{
//需要动画
if (animated)
{
[UIView animateWithDuration:0.6 animations:^{
_BackgroundView.alpha = 1.0;
_BackgroundView.transform = trans;
}];
}
//没有动画
else
{
_BackgroundView.alpha = 1.0;
_BackgroundView.transform = trans;
}
}
}
- 自定义的TabBar 视图,一般都是通过按钮代替实现,也可以通过Controller实现,我这里用得是按钮。
#pragma mark - === 自定义的TabBar ==
- (void)creationCusTabBar
{
_titles = @[@"测试一",@"测试二",@"测试三"];
NSArray * norImages = @[@"tabbar_contacts",@"tabbar_discover",@"tabbar_mainframe"];
NSArray * seleImages = @[@"tabbar_contactsHL",@"tabbar_discoverHL",@"tabbar_mainframeHL"];
//按钮宽度
CGFloat width = ScreenWidth / _titles.count;
for (NSInteger i = 0; i<_titles.count; i++)
{
//按钮:
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:[norImages objectAtIndex:i]] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:[seleImages objectAtIndex:i]] forState:UIControlStateSelected];
btn.titleLabel.font = [UIFont systemFontOfSize:12];
[btn setTitle:[_titles objectAtIndex:i]forState:UIControlStateNormal];
[btn setTintColor:[UIColor blackColor]];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
btn.tag = i+100;
[btn addTarget:self action:@selector(TapBnt:) forControlEvents:UIControlEventTouchUpInside];
if (i == 1)
{
btn.frame = CGRectMake(width*i, 20, width, 40);
}
else
{
btn.frame = CGRectMake(width*i, 0, width, 40);
}
[_imageView addSubview:btn];
}
}
- 如何切换控制器
#pragma mark - === 逻辑处理 ====
/**
* 点击按钮,切换控制器:
*/
- (void)TapBnt: (UIButton *)sender
{
for (int i = 0; i < _titles.count; i++)
{
UIButton *btn = (UIButton *)[_imageView viewWithTag:i+100];
btn.selected = NO;
}
sender.selected = !sender.selected;
//切换控制器:
self.selectedIndex = sender.tag - 100;
}
部分代码,只是为了用于笔记记录,感谢大神们指点。代码地址