How to increase height of UIProgressView

Viewed 73739

I am creating UIProgressView from nib. I want to increase its height but it is fixed to 9. For iPad I need to increase its height. How it can be done?

23 Answers

Use CGAffineTransform to change dimensions:

CGAffineTransform transform = CGAffineTransformMakeScale(1.0f, 3.0f);  
progressView.transform = transform;

Just subclass and adjust the intrinsic size:

class FatProgressView: UIProgressView {

    override var intrinsicContentSize: CGSize {
        return CGSize(width: UIView.noIntrinsicMetric, height: 20.0)
    }

}

We can set the height of UIProgressView by creating the custom class of ProgressView by subclassing the UIProgressView.

In Swift,

public class ProgressView: UIProgressView {
    var height: CGFloat = 4.0
    public override func sizeThatFits(_ size: CGSize) -> CGSize {
        return CGSize(width: size.width, height: height) // We can set the required height
    }
}

Alternately, one could implement the layout height programmatically.

private var _progress: UIProgressView!

    // ...
    _vstack.translatesAutoresizingMaskIntoConstraints = false

    let progressHeightAnchor = _progress.heightAnchor
        .constraint(equalToConstant: 16.0)
    
    NSLayoutConstraint.activate([progressHeightAnchor /* , ...  */ ])

I was just looking for the same problem and got it working with just subclassing UIProgressView and overriding sizeThatFits method, works like a charm.

class BigProgressView: UIProgressView {
    override func sizeThatFits(_ size: CGSize) -> CGSize {
        return CGSize(width: size.width, height: 5)
    }
}
Related