How to make rounded corner progress bar in swift?

前端 未结 5 1927
长情又很酷
长情又很酷 2021-01-03 21:24

Here I Have tried to make Rounded Rect corner progress bar but I have some problem to create it, here I have post my code what I am tried?

Any one Give idea to custo

相关标签:
5条回答
  • 2021-01-03 21:53

    UIProgressView changed position of UIImageView which show current value of progress bar. I have small update to fix this issue

     self.layer.cornerRadius = 12
     self.clipsToBounds = true
     for subview in subviews {
        if let imageView = subview as? UIImageView {
           imageView.layer.cornerRadius = 12
           imageView.clipsToBounds = true
        }
     }
    
    0 讨论(0)
  • 2021-01-03 21:58

    Although you have set the corner radius, you also need to tell the view not to draw anything outside of the view's bounds by setting

    self.progressView.clipsToBounds = true
    
    0 讨论(0)
  • 2021-01-03 22:04

    And if you want to have rounded edges for the inner bar too, you can also add this code:

    // Set the rounded edge for the outer bar
    self.layer.cornerRadius = 12
    self.clipsToBounds = true
    
    // Set the rounded edge for the inner bar        
    self.layer.sublayers![1].cornerRadius = 12
    self.subviews[1].clipsToBounds = true
    
    0 讨论(0)
  • 2021-01-03 22:05

    Swift 5.0

    //Extension to set corner any view  set border width and color. 
    extension UIView{
        func setCorner(withRadius:Int, borderWidth:Int = 0, color: UIColor = .clear){
            self.layer.cornerRadius = radius
            self.layer.borderColor = color
            self.layer.borderWidth = borderWidth
            self.clipsToBounds = true
        }
    }
    

    use it as

    self.progressView.setCorner(withRadius: 12)
    
    0 讨论(0)
  • 2021-01-03 22:14

    In which it tells you to set the corner radius and then clip to bounds: (The sublayers is so the inside bar has rounded corners as well.) soo add these lines because you need to set both the progress color and his layer also...

     progressBar.layer.cornerRadius = 8
     progressBar.clipsToBounds = true
     progressBar.layer.sublayers![1].cornerRadius = 8
     progressBar.subviews[1].clipsToBounds = true
    
    0 讨论(0)
提交回复
热议问题