Home » Swift » Change Height of UIProgressView in Swift

Change Height of UIProgressView in Swift

Posted by: admin November 30, 2017 Leave a comment

Questions:

I’ve been trying to change the size and rotation of a progress view to adopt the size of the screen. It would basically function have a filling glass effect on the screen of the phone.

I rotated it fairly harmlessly by using
self.masteryProgress.transform = CGAffineTransformMakeRotation((CGFloat(-90) / CGFloat(180.0) * CGFloat(M_PI)))

I got the size of the view encasing the screen using view.bounds trying to get the rect and the width x height. So I’m not stuck there.

I tried using .frame .drawRect but when I use those it either breaks them or does nothing.

The height attribute seems to be locked at a constant value of 2 in interfaceBuilder.

Any solutions before I build a custom animation?

I tried setting the height of the progress bar in interfaceBuilder like some other posts said, but I need to have the progress view fill the whole screen no matter what device it’s running on so that solution won’t work in my case.

I ended up using
CGAffineTransformScale(masteryProgress.transform, view.bounds.height / 1334, (view.bounds.width / 2))

Since I’m rotating the progressView vertically, I set the progress view to a width of 1334 (the height of the iPhone 6 Plus) and a height of 2. By dividing the height of the screen by these values it should resize to any phone perfectly.

Answers:

You can scale it by set its transform like so:

Swift 2

masteryProgress.transform = CGAffineTransformScale(masteryProgress.transform, 1, 20)

Swift 3、4

masteryProgress.transform = masteryProgress.transform.scaledBy(x: 1, y: 20)

enter image description here

Questions:
Answers:

Transform still seems to be the only way to accomplish this. Try adding it as an extension to UIProgressView like so.

extension UIProgressView {

    @IBInspectable var barHeight : CGFloat {
        get {
            return transform.d * 2.0
        }
        set {
            // 2.0 Refers to the default height of 2
            let heightScale = newValue / 2.0
            let c = center
            transform = CGAffineTransformMakeScale(1.0, heightScale)
            center = c
        }
    }   
}

Questions:
Answers:

This will work:

masteryProgress.transform = CGAffineTransformMakeScale(1, 4)

Questions:
Answers:

If you added UIProgressView using Interface Builder, just create height constraint and change to value what you need. That’s it.

Questions:
Answers:

In Swift version 3.0.2 use this:

masteryProgress.transform = masteryProgress.transform.scaledBy(x: 1, y: 5)

Questions:
Answers:

In Swift 3, it’s changed to CGAffineTransform(scaleX: CGFloat, y: CGFloat). So code would be:

masteryProgress.transform = CGAffineTransform(scaleX: 1, y: 4)

Questions:
Answers:

I’m experiencing same with Swift – UIProgressView fills diagonally

After scale progressbar with

CGAffineTransformMakeScale(1.0, 5.0)

bar animation becomes filling from left top corner to right bottom.