How to create a non-rounded corner UIProgressView in iOS

I subclass UIProgressView as:

import UIKit

class MyProgressView: UIProgressView {

    override func sizeThatFits(size: CGSize) -> CGSize {
        return CGSizeMake(size.width, 6)
    }
}

and I use it like:

let progress = MyProgressView()

progress.progress = 0.33
progress.layer.cornerRadius = 0
progress.tintColor = .white
progress.trackTintColor = UIColor.white.colorWithAlphaComponent(0.4)
navigationItem.titleView = progress

it works fine but has rounded corners as shown below

progress view with rounded corners

I want it to be not a rounded corner. How can i do this?

+9
source share
2 answers

Just change to , by default . progressViewStyleUIProgressView.Style.barUIProgressView.Style.default

Swift 3.0 and above

self.progressViewStyle = .bar /* default is .default */

Swift 2.0

self.progressViewStyle = UIProgressViewStyle.Bar
+21
source
class ProgressView: UIView {
    var progress: Float {
        get {
            return self.progress
        }
        set(progress) {
            self.progress = progress
            self.setNeedsLayout()
        }
    }
    var progressBar: UIView
    override func layoutSubviews() {
        var frame: CGRect = self.bounds
        frame.size.width *= progress
        progressBar.frame = frame
    }
}
-2
source

Source: https://habr.com/ru/post/1651069/


All Articles