如何只在圈UIButton顶部制作边框颜色?

时间:2016-06-13 11:18:40

标签: ios swift colors uitabbarcontroller border-color

我正在UIButton制作中心圈UITabBarController。我只需要在UIButton从tabBar边框出来的地方绘制边框颜色。我该怎么做?我们只需要https://monosnap.com/file/7MDqGzpUdIbClvnAvAiY2kJYKUro7z

的边框颜色

我做了像UIButton

       private func setupLifelineButton() {
        let lifelineButton = UIButton(frame: CGRect(x: 0, y: 0, width: 64, height: 64))
        var lifelineButtonFrame = lifelineButton.frame
        lifelineButtonFrame.origin.y = view.bounds.height - lifelineButtonFrame.height - 13 // default without 13
        lifelineButtonFrame.origin.x = view.bounds.width / 2 - lifelineButtonFrame.size.width / 2
        lifelineButton.frame = lifelineButtonFrame

//        lifelineButton.backgroundColor = .redColor()
        lifelineButton.layer.cornerRadius = lifelineButtonFrame.height / 2
        lifelineButton.layer.borderWidth = 0.5
        lifelineButton.layer.borderColor = ColorManager.tabBarLayerColor.CGColor//UIColor.blackColor().CGColor

        lifelineButton.addTarget(self, action: #selector(menuButtonAction), forControlEvents: .TouchUpInside)

        // icon 

//        lifelineButton.setImage(UIImage(named: "LifeLineBarButtonIcon"), forState: .Normal)

        self.view.addSubview(lifelineButton)
        self.view.layoutIfNeeded()
    }

更新:我需要删除边框颜色的下半部分

3 个答案:

答案 0 :(得分:2)

这可能对您有所帮助

目标c

-(void)createCurveBtnWithBorder
{
   UIBezierPath *shapePath = [UIBezierPath bezierPathWithRoundedRect:_btnCurve.bounds
                                                byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
                                                      cornerRadii:CGSizeMake(_btnCurve.frame.size.width/2, _btnCurve.frame.size.height/2)];

   CAShapeLayer *shapeLayer = [CAShapeLayer layer];
   shapeLayer.frame = _btnCurve.bounds;
   shapeLayer.path = shapePath.CGPath;
   _btnCurve.backgroundColor=[UIColor clearColor];
   shapeLayer.fillColor = [UIColor purpleColor].CGColor;
   shapeLayer.strokeColor = [UIColor blueColor].CGColor; //Here you can set border with green color
   shapeLayer.lineWidth = 2;
   [_btnCurve.layer addSublayer:shapeLayer];
}

Swift 4

func createCurveBtnWithBorder()
{
   let shapePath = UIBezierPath(roundedRect: btnCurve.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: btnCurve.frame.size.width / 2, height: btnCurve.frame.size.height / 2))
   let shapeLayer = CAShapeLayer()
   shapeLayer.frame = btnCurve.bounds
   shapeLayer.path = shapePath.cgPath
   btnCurve.backgroundColor = UIColor.clear as? CGColor
   shapeLayer.fillColor = UIColor.purple.cgColor
   shapeLayer.strokeColor = UIColor.blue.cgColor
   //Here you can set border with green color
   shapeLayer.lineWidth = 2
   btnCurve.layer.addSublayer(shapeLayer)
}

这是输出

enter image description here

答案 1 :(得分:0)

那么你可以准确地制作出你想要的图像并给出图像按钮!

答案 2 :(得分:0)

我做了不同的事情

     private func setupLifelineButton() {
        lifelineButton = UIButton(frame: CGRect(x: 0, y: 0, width: 64, height: 64))
        var lifelineButtonFrame = lifelineButton.frame
        lifelineButtonFrame.origin.y = view.bounds.height - lifelineButtonFrame.height - 13 // default without 13
        lifelineButtonFrame.origin.x = view.bounds.width / 2 - lifelineButtonFrame.size.width / 2
        lifelineButton.frame = lifelineButtonFrame

        lifelineButton.backgroundColor = .whiteColor()
        lifelineButton.layer.cornerRadius = lifelineButtonFrame.height / 2

        lifelineButton.addTarget(self, action: #selector(showLifelineScreen), forControlEvents: .TouchUpInside)

        // icon 
        let lifelineImageView = UIImageView(image: UIImage(named: "LifeLineBarButtonIcon"))
        view.addSubview(lifelineButton)
        lifelineButton.addSubview(lifelineImageView)

        lifelineImageView.snp_makeConstraints { (make) in
            make.width.equalTo(ConstraintManager.widthByMainScreenByOriginalDigit(30))
            make.height.equalTo(ConstraintManager.heightByMainScreenByOriginalDigit(40))
            make.center.equalTo(lifelineButton)
        }


        // second view
        let path = UIBezierPath(arcCenter: CGPoint(x: lifelineButton.frame.width / 2, y: lifelineButton.frame.height / 2), radius: lifelineButtonFrame.height / 2, startAngle: CGFloat(-M_PI_2), endAngle: CGFloat(M_PI * 2 - M_PI_2), clockwise: true)

        let arc = Arc.sharedInstance
        arc.addFigure(path.CGPath, fillColor: .clearColor(), strokeColor: ColorManager.tabBarLayerColor, strokeStart: 0, strokeEnd: 0.23, lineWidth: 0.5, miterLimit: 0, lineDashPhase: 0, layer: lifelineButton.layer)
        arc.addFigure(path.CGPath, fillColor: .clearColor(), strokeColor: .whiteColor(), strokeStart: 0.23, strokeEnd: 0.77, lineWidth: 0.5, miterLimit: 0, lineDashPhase: 0, layer: lifelineButton.layer)
        arc.addFigure(path.CGPath, fillColor: .clearColor(), strokeColor: ColorManager.tabBarLayerColor, strokeStart: 0.77, strokeEnd: 1, lineWidth: 0.5, miterLimit: 0, lineDashPhase: 0, layer: lifelineButton.layer)

        view.layoutIfNeeded()
    }

public class Arc {

    public init() { }

    public static let sharedInstance = Arc()

    public func addFigure(path: CGPath, fillColor: UIColor, strokeColor: UIColor, strokeStart: CGFloat, strokeEnd: CGFloat, lineWidth: CGFloat, miterLimit: CGFloat, lineDashPhase: CGFloat, layer: CALayer) {
        let shape = CAShapeLayer()
        shape.path = path
        shape.fillColor = fillColor.CGColor
        shape.strokeColor = strokeColor.CGColor
        shape.strokeStart = strokeStart
        shape.strokeEnd = strokeEnd
        shape.lineWidth = lineWidth
        shape.miterLimit = miterLimit
        shape.lineDashPhase = lineDashPhase
        layer.addSublayer(shape)
    }

    public func drawCircle(arcRadius: CGFloat, miniRadius: CGFloat, fillColor: UIColor, strokeColor: UIColor, view: UIView) {

    }
}

我的结果enter image description here