iOS 中间按钮突出的tabbar

本文介绍了如何在iOS应用中实现一个中间按钮突出的TabBar效果。通过自定义View重写drawRect方法,利用贝塞尔曲线绘制背景图,并通过设置TabBarController调整按钮的偏移量来达到所需样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于项目需求,需要写一个下图的tabbar,经过百度,解决方案如下


中间按钮突出

使用贝塞尔曲线来画一个tabbar的背景图,自定义一个view 重写view的drawRect方法,代码如下:


-(void)drawRect:(CGRect)rect{

    CGFloat lineY =0;

    CGFloat radiu =0;

    CGFloat lineWidth =0.5;

    lineY = rect.size.height - lineWidth -50;

    radiu = (rect.size.height - lineWidth *2)/2.0;

//1.左边线条

    UIColor *coloerOne = [UIColorcolorWithRed:0green:0blue:0alpha:0.5];

    [coloerOne set];

    //圆心到左右横线的垂直距离

    CGFloat toTop = radiu - lineY + lineWidth;

     //勾股定理

    CGFloat all =pow(radiu,2)-pow(toTop,2);

    //对边长(有两个,所以 * 2

    CGFloat x2 =sqrt(all)*2;

    //线条宽度(视图宽度减去所占的宽度,除以2得到一边的宽度)

    CGFloat line1W = (rect.size.width - x2)/2.0;

    //圆左边点

    CGPoint leftPoint =CGPointMake(line1W, lineY);

    

    UIBezierPath *leftLinePath = [UIBezierPathbezierPath];

    // 起点

    [leftLinePath moveToPoint:CGPointMake(0, lineY)];

    // 其他点

    [leftLinePath addLineToPoint:leftPoint];

    leftLinePath.lineWidth = lineWidth;

    leftLinePath.lineCapStyle =kCGLineCapRound;//线条拐角

    leftLinePath.lineJoinStyle =kCGLineJoinRound;//终点处理

    

    [leftLinePath stroke];

    

//    右边线条

    UIColor *coloerTwo = [UIColorcolorWithRed:0green:0blue:0alpha:0.5];

    [coloerTwo set];

    CGFloat rX = rect.size.width - line1W;

    UIBezierPath *rightLinePath = [UIBezierPathbezierPath];

    //圆右边点

    CGPoint rightPoint =CGPointMake(rX, lineY);

    [rightLinePath moveToPoint:rightPoint];

    // 其他点

    [rightLinePath addLineToPoint:CGPointMake(rect.size.width, lineY)];

    rightLinePath.lineWidth = lineWidth;

    rightLinePath.lineCapStyle =kCGLineCapRound;//线条拐角

    rightLinePath.lineJoinStyle =kCGLineJoinRound;//终点处理

    [rightLinePath stroke];

    

//    弧线

    UIColor *coloerThree = [UIColorcolorWithRed:0green:0blue:0alpha:0.5];

    [coloerThree set];

    CGFloat yyy2 =acos(toTop / radiu);

    CGPoint centerPoint =CGPointMake( rect.size.width /2, rect.size.height /2);

    UIBezierPath *roundLinePath = [UIBezierPathbezierPath];

//    clockwise:是否顺时针绘圆

    [roundLinePath addArcWithCenter:centerPointradius:radiustartAngle:-yyy2 -M_PI_2endAngle:yyy2 -M_PI_2clockwise:true];

    roundLinePath.lineWidth = lineWidth;

    roundLinePath.lineCapStyle =kCGLineCapRound;//线条拐角

    roundLinePath.lineJoinStyle =kCGLineJoinRound;//终点处理

    [roundLinePath stroke];

    

//   镂空,去除上边白色部分

    //获取直线下面矩形范围路径

    UIBezierPath *ppp = [UIBezierPathbezierPathWithRect:CGRectMake(0, lineY-lineWidth, rect.size.width, rect.size.height-lineY)];

    //获取圆弧所在圆路径

    [ppp appendPath:[UIBezierPathbezierPathWithRoundedRect:CGRectMake((rect.size.width-rect.size.height)/2,0, rect.size.height, rect.size.height)cornerRadius:radiu]];

    CAShapeLayer *shapeLayer = [[CAShapeLayeralloc]init];

    shapeLayer.path = ppp.CGPath;

    self.layer.mask = shapeLayer;

    

}

再写自定义一个TabBarController继承 UITabBarController,这里需要设置tabbar上按钮的偏移

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    _myHeight =70;

    _viewArray = [[NSMutableArrayalloc]initWithCapacity:0];

    [selfclearTabBarTopLine];

    CCTabbarBackView *bgV =[[CCTabbarBackViewalloc]initWithFrame:CGRectMake(0,0,UIScreen.mainScreen.bounds.size.width,_myHeight)];

    bgV.backgroundColor = [UIColorwhiteColor];

    [self.tabBaraddSubview:bgV];

    [selfcreatSubViewCtrs];

}

//除去黑线

-(void)clearTabBarTopLine{

    CGRect rect =CGRectMake(0,0,UIScreen.mainScreen.bounds.size.width,UIScreen.mainScreen.bounds.size.height);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context =UIGraphicsGetCurrentContext();

    CGColorRef clearColor =[[UIColorclearColor] CGColor];

    CGContextSetFillColor(context,CGColorGetComponents(clearColor));

    CGContextFillRect(context, rect);

    UIImage *img =UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    self.tabBar.backgroundImage = img;

    self.tabBar.shadowImage = img;

}

如果没有除去黑线,则会出现下图的效果

//设置按钮图片偏移量

-(void)viewWillLayoutSubviews{

    CGRect frame =self.tabBar.frame;

    frame.size.height =_myHeight;

    frame.origin.y =self.view.frame.size.height - _myHeight;

    self.tabBar.frame = frame;

    self.tabBar.barStyle =UIBarStyleDefault;

    for (int i=0; i<self.tabBar.items.count; i++) {

        UITabBarItem *item =self.tabBar.items[i];

        if (i!=2) {

            item.imageInsets =UIEdgeInsetsMake(_myHeight-50-10,0, -(_myHeight -50 -10),0);

            item.titlePositionAdjustment =UIOffsetMake(0, -3);


        }else{

            item.imageInsets =UIEdgeInsetsMake(0,0,0,0);

            item.titlePositionAdjustment =UIOffsetMake(0, -3);


        }

    }

}

点击打开demo链接


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值