Making UIProgressView Rounded corners

前端 未结 13 1688
旧巷少年郎
旧巷少年郎 2021-02-07 13:59

I have created a UIProgressView with following properties

progressView.progressTintColor = UIColor.appChallengeColorWithAlpha(1.0)
progressView.trac         


        
相关标签:
13条回答
  • 2021-02-07 14:53

    Just do this in init

        layer.cornerRadius = *desired_corner_radius*
        clipsToBounds = true
    
    0 讨论(0)
  • 2021-02-07 14:54

    It's very late to answer but actually I had the same problem.

    Here my simplest solution (no code needed !) :

    1. Add a container to embed your progress view

    1. Round corner for your container (10 = height of container / 2)

    1. The result :)

    0 讨论(0)
  • After searching and trying I decided to create my own custom progress view. Here is the code for anyone who may find them selevs in same problem.

    import Foundation
    import UIKit
    
    class CustomHorizontalProgressView: UIView {
    var progress: CGFloat = 0.0 {
    
        didSet {
            setProgress()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    
        setup()
    }
    
    func setup() {
        self.backgroundColor = UIColor.clearColor()
    }
    
    override func drawRect(rect: CGRect) {
        super.drawRect(rect)
    
        setProgress()
    }
    
    func setProgress() {
        var progress = self.progress
        progress = progress > 1.0 ? progress / 100 : progress
    
        self.layer.cornerRadius = CGRectGetHeight(self.frame) / 2.0
        self.layer.borderColor = UIColor.grayColor().CGColor
        self.layer.borderWidth = 1.0
    
        let margin: CGFloat = 6.0
        var width = (CGRectGetWidth(self.frame) - margin)  * progress
        let height = CGRectGetHeight(self.frame) - margin
    
        if (width < height) {
            width = height
        }
    
        let pathRef = UIBezierPath(roundedRect: CGRect(x: margin / 2.0, y: margin / 2.0, width: width, height: height), cornerRadius: height / 2.0)
    
        UIColor.redColor().setFill()
        pathRef.fill()
    
        UIColor.clearColor().setStroke()
        pathRef.stroke()
    
        pathRef.closePath()
    
        self.setNeedsDisplay()
    }
    }
    

    Just put above code in a swift file and drag drop a UIView in IB and give it class CustomHorizontalProgressView. and That is it.

    0 讨论(0)
  • 2021-02-07 14:56

    Swift 4.2 version from Umair Afzal's solution

    class CustomHorizontalProgressView: UIView {
    
    var strokeColor: UIColor?
    
    var progress: CGFloat = 0.0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    
    func setup() {
        self.backgroundColor = UIColor.clear
    }
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        setProgress()
    }
    
    func setProgress() {
    
        var progress = self.progress
        progress = progress > 1.0 ? progress / 100 : progress
    
        self.layer.cornerRadius = frame.size.height / 2.0
        self.layer.borderColor = UIColor.gray.cgColor
        self.layer.borderWidth = 1.0
    
        let margin: CGFloat = 6.0
        var width = (frame.size.width - margin)  * progress
        let height = frame.size.height - margin
    
        if (width < height) {
            width = height
        }
    
        let pathRef = UIBezierPath(roundedRect: CGRect(x: margin / 2.0, y: margin / 2.0, width: width, height: height), cornerRadius: height / 2.0)
    
        strokeColor?.setFill()
    
        pathRef.fill()
        UIColor.clear.setStroke()
        pathRef.stroke()
        pathRef.close()
    }
    }
    

    And to use it

    var progressView: CustomHorizontalProgressView = {
        let view = CustomHorizontalProgressView()
        view.strokeColor = UIColor.orange
        view.progress = 0.5
        return view
    }()
    
    0 讨论(0)
  • 2021-02-07 14:59

    //Updated for swift 4

    import Foundation
    import UIKit
    
    class CustomHorizontalProgressView: UIView {
    var progress: CGFloat = 0.0 {
    
        didSet {
            setProgress()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    
        setup()
    }
    
    func setup() {
        self.backgroundColor = UIColor.clear
    }
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
    
        setProgress()
    }
    
    func setProgress() {
        var progress = self.progress
        progress = progress > 1.0 ? progress / 100 : progress
    
        self.layer.cornerRadius = self.frame.height / 2.0
        self.layer.borderColor = UIColor.gray.cgColor
        self.layer.borderWidth = 1.0
    
        let margin: CGFloat = 6.0
        var width = (self.frame.width - margin)  * progress
        let height = self.frame.height - margin
    
        if (width < height) {
            width = height
        }
    
        let pathRef = UIBezierPath(roundedRect: CGRect(x: margin / 2.0, y: margin / 2.0, width: width, height: height), cornerRadius: height / 2.0)
    
        UIColor.red.setFill()
        pathRef.fill()
    
        UIColor.clear.setStroke()
        pathRef.stroke()
    
        pathRef.close()
    
        self.setNeedsDisplay()
     }
    }
    
    0 讨论(0)
  • 2021-02-07 15:01

    Basically progress view's (Default Style) subviews consist of 2 image view. One for the "progress", and one for the "track". You can loop the subviews of progress view, and set the each of image view's corner radius.

    for let view: UIView in self.progressView.subviews {
        if view is UIImageView {
            view.clipsToBounds = true
            view.layer.cornerRadius = 15
        }
    }
    
    0 讨论(0)
提交回复
热议问题