How to increase height of UIProgressView

IphoneCocoa TouchIpadUiprogressview

Iphone Problem Overview


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?

Iphone Solutions


Solution 1 - Iphone

Use CGAffineTransform to change dimensions:

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

Solution 2 - Iphone

Using layout constraints worked for me:

enter image description here

Solution 3 - Iphone

place this source

@implementation UIProgressView (customView)
- (CGSize)sizeThatFits:(CGSize)size {
    CGSize newSize = CGSizeMake(self.frame.size.width, 9);
    return newSize;
}
@end

Solution 4 - Iphone

Just set the frame of the UIProgressView in code after it has been initialised. Eg:

UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
// progressView's frame will be small/standard height

progressView.frame = CGRectMake(0, 0, 100, 20);
// Now the frame is set to my custom rect.

This is working for me on iOS 5. I'm using custom trackImage and progressImage though. My code looks like this. Note that I'm adding the progressView to another view and want it to be the same size as the containing view hence setting the frame to self.bounds.

_progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
_progressView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_progressView.trackImage = [[UIImage imageNamed:@"search-progress-track"] resizableImageWithCapInsets:UIEdgeInsetsMake(3.0f, 3.0f, 3.0f, 3.0f)];
_progressView.progressImage = [[UIImage imageNamed:@"search-progress"] resizableImageWithCapInsets:UIEdgeInsetsMake(3.0f, 3.0f, 3.0f, 3.0f)];
_progressView.frame = self.bounds;
[_progressView setProgress:0.5];

Solution 5 - Iphone

There is a simple way to change the height within Interface Builder. No code required and your change will show in IB.

Select the UIProgressView object in your storyboard or xib, choose Editor > Pin > Height. This will create a height constraint and allow you to change its value in the Attributes Inspector in your left most (Utilizes) panel.

Solution 6 - Iphone

The following code works on the newest swift xCode:

var transform : CGAffineTransform = CGAffineTransformMakeScale(1.0, 6.0)
           progressView.transform = transform
    

Solution 7 - Iphone

Swift 3:

progressView.transform = progressView.transform.scaledBy(x: 1, y: 9)

Solution 8 - Iphone

Swift3

var transform : CGAffineTransform = CGAffineTransform(scaleX: 1.0, y: 6.0)
progressBar.transform = transform 

Addendum

If you're working with an IBDesignable class, you can tweak it with this from your storyboard:

@IBInspectable var progressBarHeight: CGFloat = 2.0 {
    didSet {
        let transform = CGAffineTransform(scaleX: 1.0, y: progressBarHeight)
        self.progressView.transform = transform
    }
}

Solution 9 - Iphone

You can not change the height of UIProgressView through nib. If you want to change the height then you have to implement it through custom draw method.

Solution 10 - Iphone

You can also use AutoLayout to achieve the same look and it works in iOS 7.1. Simply add a height constraint equal to the height you want your progress bar to be. Check out this answer on this similar question for more details.

https://stackoverflow.com/a/19606981/142358

Solution 11 - Iphone

Here is the Solution

You can use transform, but problem arises that -> when you change orientation of the device, UIProgressView height becomes original one.

So best way to increase UIProgressView height is

yourProgressView.progressImage=[UIImage imageNamed:@"image1.png"];
yourProgressView.trackImage = [UIImage imageNamed:@"image2.png"];
// IMPORTANT: image1/image2 height (in pixel) = height that you want for yourProgressView.
// no need to set other properties of yourProgressView.

Thank you

Solution 12 - Iphone

For iOS 7+, use Core Graphics and CATransform3DScale to scale the layer in that view:

CATransform3D transform = CATransform3DScale(progressView.layer.transform, 1.0f, 3.0f, 1.0f);
progressView.layer.transform = transform;

Just make sure you do this after you set the frame of progressView, not before.

Solution 13 - Iphone

For iOS 7 and above, I did the following which (a) works and (b) does not generate a warning:

First add UIProgressView to my View Controller in the storyboard. Then add a Height constraint to the UIProgressView by CTRL+Dragging the UIProgressView to itself. The constraint will be created with a default value of 2. Leave that alone.

Now to change the height add an IBOutlet to the UIViewController subclass in code like this:

@property (nonatomic, weak) IBOutlet NSLayoutConstraint *progressHeight;

Then in your code, probably - viewDidLoad, add:

self.progressHeight.constant = 9;

This should work out nicely for you.

Solution 14 - Iphone

Just subclass and adjust the intrinsic size:

class FatProgressView: UIProgressView {

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

Solution 15 - Iphone

Here is a third party progress bar that allows setting height. https://github.com/yannickl/YLProgressBar

enter image description here

Set frame via code or interface builder. You might want to disable the animation or stripes, though.

Here is code for a thick green progress bar:

YLProgressBar *bar = [[YLProgressBar alloc] initWithFrame:CGRectMake(0, 100, 100, 50)];
bar.progress = 0.5;
bar.type = YLProgressBarTypeFlat;
bar.hideStripes = YES;
bar.behavior = YLProgressBarBehaviorDefault;
bar.progressTintColors = @[[UIColor greenColor], [UIColor greenColor]];

enter image description here

Solution 16 - Iphone

You can implement a category (new file - category) and just add the category at the beginning of your class. It works fine also with iboutlet (nib/storyboard).

The code is just

@interface UIProgressView (heavyView)
@end

@implementation UIProgressView (heavyView)
- (CGSize)sizeThatFits:(CGSize)size
{
    CGSize newSize = CGSizeMake(size.width, 9);
    return newSize;
}
@end

if you want to apply the change for just one progressView and you have more than one progressView in your class, you can just use a subclass instead.

Solution 17 - Iphone

Mayur's method worked well for me in iOS 7, here's my code (uses UIImage+BBlock)

    CGFloat progressViewHeight = 5;
    [[UIProgressView appearance] setProgressImage:[UIImage imageForSize:CGSizeMake(1, progressViewHeight) withDrawingBlock:^{
        CGContextRef context = UIGraphicsGetCurrentContext();
        UIColor * colortoUse = [UIColor blueColor];
        CGContextSetFillColorWithColor(context, [colortoUse CGColor]);
        CGContextFillRect(context, CGRectMake(0, 0, 1, progressViewHeight));
    }]];
    
    [[UIProgressView appearance] setTrackImage:[UIImage imageForSize:CGSizeMake(1, progressViewHeight) withDrawingBlock:^{
        CGContextRef context = UIGraphicsGetCurrentContext();
        UIColor * colortoUse = [UIColor progressViewBackgroundColor];
        CGContextSetFillColorWithColor(context, [colortoUse CGColor]);
        CGContextFillRect(context, CGRectMake(0, 0, 1, progressViewHeight));
    }]];

Solution 18 - Iphone

Instead of using interface builder, the height of UIProgressView can be changed by adding constraints to it programmatically.

UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
progressView.translatesAutoresizingMaskIntoConstraints = NO;
CGRect frame = CGRectMake(100, 200, 200, 50);
[self.view addSubview:progressView];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-y-[progressView(height)]" options:0
                                                                  metrics:@{@"y": @(CGRectGetWidth(frame)),
                                                                            @"height": @(CGRectGetHeight(frame))}
                                                                    views:NSDictionaryOfVariableBindings(progressView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-x-[progressView(width)]" options:0
                                                                  metrics:@{@"x": @(CGRectGetMinX(frame)),
                                                                            @"width": @(CGRectGetWidth(frame))}
                                                                    views:NSDictionaryOfVariableBindings(progressView)]];

Solution 19 - Iphone

My suggestion would be to place a container view on your view controller's view in Auto Layout. Make sure it's the size that you want for your progress bar. Now drag a progress view inside the container and pin all sides to the bounds of the container. You should immediately see the progress view resize to fit the bounds of its container.

Resized UIProgressView

Solution 20 - Iphone

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
    }
}

Solution 21 - Iphone

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 /* , ...  */ ])

Solution 22 - Iphone

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)
    }
}

Solution 23 - Iphone

Simple. Swift 4.

progressBar.transform = CGAffineTransform(scaleX: self.view.frame.width / progressBar.frame.width, y: self.view.frame.height / progressBar.frame.height)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChandan Shetty SPView Question on Stackoverflow
Solution 1 - IphoneashaykView Answer on Stackoverflow
Solution 2 - IphoneLoganView Answer on Stackoverflow
Solution 3 - IphoneShoaibView Answer on Stackoverflow
Solution 4 - IphoneorjView Answer on Stackoverflow
Solution 5 - IphoneMattView Answer on Stackoverflow
Solution 6 - Iphoneuser4935372View Answer on Stackoverflow
Solution 7 - IphoneMohammad RazipourView Answer on Stackoverflow
Solution 8 - IphoneAmit Jagesha シView Answer on Stackoverflow
Solution 9 - IphoneraazView Answer on Stackoverflow
Solution 10 - IphoneSteve MoserView Answer on Stackoverflow
Solution 11 - IphoneMayur KothawadeView Answer on Stackoverflow
Solution 12 - IphoneMike OnoratoView Answer on Stackoverflow
Solution 13 - Iphonei_am_jorfView Answer on Stackoverflow
Solution 14 - IphoneKonstantinos KontosView Answer on Stackoverflow
Solution 15 - IphoneftvsView Answer on Stackoverflow
Solution 16 - IphoneDaniel C.View Answer on Stackoverflow
Solution 17 - IphoneJasonView Answer on Stackoverflow
Solution 18 - Iphoneuser3480295View Answer on Stackoverflow
Solution 19 - IphoneStephen PaulView Answer on Stackoverflow
Solution 20 - IphoneSathish Kumar GurunathanView Answer on Stackoverflow
Solution 21 - Iphonel --marc lView Answer on Stackoverflow
Solution 22 - IphoneSangsomView Answer on Stackoverflow
Solution 23 - IphoneTimmy SorensenView Answer on Stackoverflow