69

我正在UIProgressViewnib. 我想增加它的高度,但它固定为 9。因为iPad我需要增加它的高度。怎么做?

4

23 回答 23

174

用 CGAffineTransform 改变尺寸:

CGAffineTransform transform = CGAffineTransformMakeScale(1.0f, 3.0f);  
progressView.transform = transform;
于 2010-12-21T23:39:44.617 回答
97

使用布局约束对我有用:

在此处输入图像描述

于 2014-09-24T21:11:08.717 回答
26

放置这个来源

@implementation UIProgressView (customView)
- (CGSize)sizeThatFits:(CGSize)size {
    CGSize newSize = CGSizeMake(self.frame.size.width, 9);
    return newSize;
}
@end
于 2014-01-10T10:19:48.197 回答
12

初始化后,只需在代码中设置 UIProgressView 的框架。例如:

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.

这在 iOS 5 上对我有用。不过,我正在使用自定义 trackImage 和 progressImage。我的代码看起来像这样。请注意,我将progressView 添加到另一个视图并希望它与包含视图的大小相同,因此将框架设置为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];
于 2012-06-01T07:15:19.010 回答
12

有一种简单的方法可以在 Interface Builder 中更改高度。无需代码,您的更改将显示在 IB 中。

在 Storyboard 或 xib 中选择 UIProgressView 对象,选择 Editor > Pin > Height。这将创建一个高度约束,并允许您在最左侧(Utilizes)面板的 Attributes Inspector 中更改其值。

于 2014-01-06T22:05:26.260 回答
12

以下代码适用于最新的 swift xCode:

var transform : CGAffineTransform = CGAffineTransformMakeScale(1.0, 6.0)
           progressView.transform = transform
于 2015-05-25T02:58:05.073 回答
11

斯威夫特 3:

progressView.transform = progressView.transform.scaledBy(x: 1, y: 9)
于 2017-01-14T13:09:18.997 回答
8

斯威夫特3

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

附录

如果你正在使用一个IBDesignable类,你可以从你的故事板中调整它:

@IBInspectable var progressBarHeight: CGFloat = 2.0 {
    didSet {
        let transform = CGAffineTransform(scaleX: 1.0, y: progressBarHeight)
        self.progressView.transform = transform
    }
}
于 2017-05-26T08:57:18.677 回答
7

您不能通过 nib 更改 UIProgressView 的高度。如果要更改高度,则必须通过自定义绘制方法来实现。

于 2010-08-09T06:03:21.033 回答
7

您还可以使用 AutoLayout 来实现相同的外观,它适用于 iOS 7.1。只需添加一个高度约束,该约束等于您希望进度条的高度。有关更多详细信息,请查看此类似问题的答案。

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

于 2014-03-28T13:32:43.867 回答
6

这是解决方案

您可以使用变换,但出现的问题是 -> 当您更改设备的方向时,UIProgressView 高度变为原始高度。

所以增加 UIProgressView 高度的最好方法是

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.

谢谢

于 2013-03-06T12:12:49.180 回答
5

对于 iOS 7+,使用 Core Graphics 和CATransform3DScale在该视图中缩放图层:

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

只需确保在设置progressView 的框架之后执行此操作,而不是之前。

于 2014-12-08T23:14:10.673 回答
4

对于 iOS 7 及更高版本,我执行了以下操作(a)有效且(b)不生成警告:

首先UIProgressView在情节提要中添加到我的视图控制器。UIProgressView然后通过 CTRL+拖动UIProgressView到自身添加一个高度约束。将使用默认值创建约束2。别管那个。

现在要更改高度,请在代码中添加一个IBOutletUIViewController类,如下所示:

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

然后在您的代码中,可能- viewDidLoad添加:

self.progressHeight.constant = 9;

这应该很适合你。

于 2014-10-15T19:13:48.147 回答
3

只需子类化并调整内在大小:

class FatProgressView: UIProgressView {

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

}
于 2019-07-03T11:44:19.137 回答
2

这是允许设置高度的第三方进度条。 https://github.com/yannickl/YLProgressBar

在此处输入图像描述

通过代码或界面生成器设置框架。不过,您可能想要禁用动画或条纹。

这是一个粗绿色进度条的代码:

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]];

在此处输入图像描述

于 2015-07-01T05:28:26.013 回答
2

您可以实现一个类别(新文件 - 类别),只需在类的开头添加类别。它也适用于 iboutlet(笔尖/故事板)。

代码只是

@interface UIProgressView (heavyView)
@end

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

如果您只想对一个progressView 应用更改并且您的班级中有多个progressView,您可以只使用一个子类。

于 2015-12-31T15:48:22.767 回答
0

Mayur 的方法在 iOS 7 中对我很有效,这是我的代码(使用 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));
    }]];
于 2014-01-22T13:59:30.003 回答
0

UIProgressView可以通过以编程方式向其添加约束来更改高度,而不是使用界面生成器。

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)]];
于 2015-07-03T14:41:06.067 回答
0

我的建议是在自动布局中的视图控制器视图上放置一个容器视图。确保它是您想要的进度条大小。现在拖动容器内的进度视图并将所有边固定到容器的边界。您应该立即看到进度视图调整大小以适合其容器的边界。

调整大小的 UIProgressView

于 2016-03-16T04:17:06.520 回答
0

UIProgressView我们可以通过创建 ProgressView 的自定义类来设置高度UIProgressView

在斯威夫特,

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
    }
}
于 2019-09-17T14:17:10.887 回答
0

或者,可以以编程方式实现布局高度。

private var _progress: UIProgressView!

    // ...
    _vstack.translatesAutoresizingMaskIntoConstraints = false

    let progressHeightAnchor = _progress.heightAnchor
        .constraint(equalToConstant: 16.0)
    
    NSLayoutConstraint.activate([progressHeightAnchor /* , ...  */ ])
于 2021-05-26T21:05:28.790 回答
0

我只是在寻找同样的问题,并让它与子类化UIProgressView和覆盖sizeThatFits方法一起工作,就像一个魅力。

class BigProgressView: UIProgressView {
    override func sizeThatFits(_ size: CGSize) -> CGSize {
        return CGSize(width: size.width, height: 5)
    }
}
于 2021-07-20T10:19:41.297 回答
-1

简单的。斯威夫特 4。

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

于 2017-07-24T21:56:10.590 回答