iOS自定义tabbar(没有tabbar上的黑线)

自定义tabbar相信在很多项目中都要用到。有的时候 还需要那种 不规则的tabbar,例如中间高两边底,例如需要添加tabbar的背景图片等等。这里 我要介绍一种 自定义tabbar的方法 ,这种方法可以调用系统的 hidesBottomBarWhenPushed 方法,很方便的隐藏tabbar。

话不多说开始正文。

第一步:
首先创建 tabbarViewController 继承自 UITabBarController

第二步:
装载视图控制器 上代码

//装载视图
-(void)addViewController
{
    UIViewController * vc1 = [[UIViewController alloc]init];
    vc1.view.backgroundColor = [UIColor redColor];
    vc1.title = @"收益权";
    UINavigationController * nvc1 = [[UINavigationController alloc]initWithRootViewController:vc1];

    ViewController * vc2 = [[ViewController alloc]init];
    UINavigationController * nvc2 = [[UINavigationController alloc]initWithRootViewController:vc2];
    vc2.view.backgroundColor = [UIColor blueColor];
    vc2.title = @"个人中心";

    ViewController * vc3 = [[ViewController alloc]init];
    UINavigationController * nvc3 = [[UINavigationController alloc]initWithRootViewController:vc3];
    vc3.view.backgroundColor = [UIColor lightGrayColor];
    vc3.title = @"论坛";
    self.viewControllers = @[nvc1,nvc2,nvc3];
}

第三部 自定义tabbar

-(void)customTab
{
    //这里是去掉 系统tabbar上黑线的方法
    self.tabBar.backgroundImage = [[UIImage alloc]init];
    self.tabBar.shadowImage = [[UIImage alloc]init];

    UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 49)];
    image.backgroundColor = [UIColor grayColor];
    image.userInteractionEnabled = YES;
    [self.tabBar addSubview:image];

    for (int i = 0;  i<3 ; i++) {
        UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3 *i +10, 5, ScreenWidth/4, 30)];
        if(i == 1)
        {
            btn.frame = CGRectMake(ScreenWidth/3 *i, -11, ScreenWidth/3, 60);
        }
        switch (i) {
            case 0:
                btn.backgroundColor = [UIColor redColor];
                break;
            case 1:
                btn.backgroundColor = [UIColor yellowColor];
                break;
            case 2:
                btn.backgroundColor = [UIColor blueColor];
                break;
            default:
                break;
        }
        btn.tag = i ;
        [btn addTarget:self action:@selector(toudown:) forControlEvents:UIControlEventTouchDown];
        [image addSubview:btn];
    }
}

不要玩了 btn的点击事件方法

-(void)toudown:(UIButton *)btn
{
    self.selectedIndex = btn.tag;
}

可以看到 自定义的tabbar 上
背景图片是一张 UIImageView ,你想要什么背景 直接找 UI 美眉要就好了。
tabbar上的 三个按钮 你想要什么样式的都行,UI切图就好了。

第四步: 完成

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addViewController];
    [self customTab];
}

在viewDidLoad里 条用这连个方法就好了。

最后附上 github 源码
https://github.com/lwq718691587/custom-tabbar

猜你喜欢

转载自blog.csdn.net/lwq718691587/article/details/50970605