69

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?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Chandan Shetty SP
  • 5,087
  • 6
  • 42
  • 63
  • 7
    I think it is time now to change the correct answer :) – Ben Groot Oct 31 '14 at 09:01
  • 1
    Some suggested applying transformation works great... but also some suggested transformation doesn't work when device rotated. So I think implementing through custom draw(may be using some open source) is correct answer. – Chandan Shetty SP Nov 14 '16 at 07:50
  • Most of the answers are kind of hack, might break on OS update (since we are trying to change the apple UI) So I think it's better to use default UIs or implement our own. – Chandan Shetty SP Oct 07 '20 at 02:27

23 Answers23

174

Use CGAffineTransform to change dimensions:

CGAffineTransform transform = CGAffineTransformMakeScale(1.0f, 3.0f);  
progressView.transform = transform;
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
ashayk
  • 1,765
  • 1
  • 10
  • 3
97

Using layout constraints worked for me:

enter image description here

Logan
  • 52,262
  • 20
  • 99
  • 128
26

place this source

@implementation UIProgressView (customView)
- (CGSize)sizeThatFits:(CGSize)size {
    CGSize newSize = CGSizeMake(self.frame.size.width, 9);
    return newSize;
}
@end
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
Shoaib
  • 1,295
  • 15
  • 22
  • This works in 7.0 and 7.1. Just create a custom UIProgressView class and added in storyboard. Change the number 9 by your needs size. Thanks !! – Beto Aug 19 '14 at 16:17
  • 7
    Actually that solution has a bad side effect. The filled part of the progress bar will be wrong. Ex, I set my progress to 0.1 (10%) but a good third gets filles because forcing the progress view to resize its height seems to wrongly resize its own progress subviews – Erwan Nov 07 '14 at 06:43
  • The "sizeThatFits" method is called a lot of time in a view. It's called before and after the resizing of the view. You need to ask to custom for new width just passed in the parameter - (CGSize)sizeThatFits:(CGSize)size { CGSize newSize = CGSizeMake(size.width, 9); return newSize; } – Daniel C. Dec 31 '15 at 15:41
12

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];
orj
  • 13,234
  • 14
  • 63
  • 73
  • 7
    This works only if: 1) you're using iOS 5.0+ 2) you're using custom images 3) you're setting the frame AFTER setting the images. – Andreas Ley Jun 19 '12 at 15:06
  • 2
    Setting the UIProgressView frame doesn't seem to work in iOS 7. – Jon Oct 31 '13 at 17:20
12

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.

Matt
  • 2,329
  • 1
  • 21
  • 23
12

The following code works on the newest swift xCode:

var transform : CGAffineTransform = CGAffineTransformMakeScale(1.0, 6.0)
           progressView.transform = transform
brodoll
  • 1,851
  • 5
  • 22
  • 25
user4935372
  • 121
  • 1
  • 2
11

Swift 3:

progressView.transform = progressView.transform.scaledBy(x: 1, y: 9)
Mohammad Razipour
  • 3,643
  • 3
  • 29
  • 49
8

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.

raaz
  • 12,410
  • 22
  • 64
  • 81
  • 34
    There should be some simpler way! – vijayst Feb 02 '14 at 13:20
  • This question is similar and has a Storyboard based solution. We are using that, but I do get warnings as a result. However it does work. I've not used the transforms suggested yet, but I like that approach better. http://stackoverflow.com/questions/18717895/progress-view-height-in-ios-7 – kvn Jan 16 '15 at 16:41
  • I can change it just fine in xcode 8.3 so maybe they changed it so you can now? – Lightsout Oct 05 '17 at 00:53
8

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
    }
}
Adrian
  • 16,233
  • 18
  • 112
  • 180
Amit Jagesha シ
  • 1,092
  • 12
  • 21
  • I made the y component an inspectable CGFloat and used this code inside the inspectable var's `didSet`. It worked beautifully. Thank you! – Adrian May 29 '17 at 13:13
7

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

Community
  • 1
  • 1
Steve Moser
  • 7,647
  • 5
  • 55
  • 94
6

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

Mayur Kothawade
  • 544
  • 4
  • 14
  • Did not work for me on Xcode 8.3.3, iOS 10, Progress Bar only showed the small height with a cropped view of the progress image – ammianus Aug 12 '17 at 15:47
5

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.

Vinh
  • 944
  • 13
  • 34
Mike Onorato
  • 1,283
  • 1
  • 10
  • 16
4

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.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
3

Just subclass and adjust the intrinsic size:

class FatProgressView: UIProgressView {

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

}
  • I tried this, but it messes up the rounded edges. It’s as if the whole thing gets stretched upwards uniformly, without preserving the rounded corners. – Nicolas Miari Nov 27 '20 at 06:44
2

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

ftvs
  • 428
  • 1
  • 6
  • 14
2

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.

Daniel C.
  • 1,534
  • 1
  • 10
  • 5
0

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));
    }]];
Jason
  • 1,323
  • 14
  • 19
0

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)]];
user3480295
  • 1,058
  • 15
  • 21
0

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

Stephen Paul
  • 2,762
  • 2
  • 21
  • 25
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
    }
}
0

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 /* , ...  */ ])
marc-medley
  • 8,931
  • 5
  • 60
  • 66
0

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)
    }
}
Sangsom
  • 373
  • 5
  • 9
-1

Simple. Swift 4.

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

Timmy Sorensen
  • 570
  • 6
  • 16