如何增加 UIProgressView 的高度

Posted

技术标签:

【中文标题】如何增加 UIProgressView 的高度【英文标题】:How to increase height of UIProgressView 【发布时间】:2010-08-09 05:34:34 【问题描述】:

我正在从nib 创建UIProgressView。我想增加它的高度,但它固定为 9。对于iPad,我需要增加它的高度。怎么做?

【问题讨论】:

我认为现在是时候改变正确答案了:) 一些建议的应用转换效果很好......但也有一些建议的转换在设备旋转时不起作用。所以我认为通过自定义绘制实现(可能使用一些开源)是正确的答案。 大部分答案都是 hack,可能会在操作系统更新时中断(因为我们正在尝试更改苹果 UI)所以我认为最好使用默认 UI 或实现我们自己的。 【参考方案1】:

使用 CGAffineTransform 改变维度:

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

【讨论】:

用比例变换它会使视图渲染变形。 改造作品!最后。以某种方式让它在 ios7 中工作。 在我的例子中,进度视图将被放大和缩小,所以另外在 y 轴上移动 progressView.origin.y 一半的比例。 请参阅下面的答案,了解适用于 iOS 7 及更高版本的解决方案。 ***.com/a/27368779/1190028 这个答案证明你大炮总是相信发帖人选择的答案。很好的答案!【参考方案2】:

使用布局约束对我有用:

【讨论】:

为什么要投反对票?这对我和至少另一个人都有效,所以如果有问题,我们可能也想知道。 这太他娘的了。来吧,苹果,如果它很容易打破你的强制高度,为什么不把它变成公共财产呢。 :) 看不出这有什么过人之处,尤其是与在代码中应用转换或设置框架相比时。 目前,XCode 7.2 会显示警告,进度视图框架在运行时会有所不同,并且不会消失 如何摆脱警告!【参考方案3】:

放置此来源

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

@end

【讨论】:

这适用于 7.0 和 7.1。只需创建一个自定义 UIProgressView 类并添加到情节提要中。根据您的需要大小更改数字 9。谢谢!! 实际上该解决方案有一个不好的副作用。进度条的填充部分会出错。例如,我将我的进度设置为 0.1 (10%),但有三分之一得到填充,因为强制进度视图调整其高度似乎错误地调整了它自己的进度子视图的大小 “sizeThatFits”方法在视图中被调用了很多时间。它在调整视图大小之前和之后调用。您需要要求自定义刚刚传入参数的新宽度 - (CGSize)sizeThatFits:(CGSize)size CGSize newSize = CGSizeMake(size.width, 9);返回新尺寸; 【参考方案4】:

初始化后,只需在代码中设置 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];

【讨论】:

这仅适用于:1)您使用的是 iOS 5.0+ 2)您使用的是自定义图像 3)您在设置图像之后设置框架。 设置 UIProgressView 框架在 iOS 7 中似乎不起作用。【参考方案5】:

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

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

【讨论】:

有关向进度视图添加高度约束的更多详细信息,请查看此答案。 ***.com/a/19606981/142358 这是在 xcode 6 中创建警告,有什么办法可以避免吗? @kl94:您可以为约束创建一个出口并在运行时设置它。请参阅下面的答案。【参考方案6】:

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

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

【讨论】:

截至 2016 年 5 月,这是正确答案。【参考方案7】:

斯威夫特 3:

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

【讨论】:

这绝对是最简单的解决方案,而且效果很好 通过这样做,progressView 有点模糊:/【参考方案8】:

Swift3

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
    

【讨论】:

我将 y 组件设为可检查的 CGFloat 并在可检查变量的 didSet 中使用此代码。它工作得很好。谢谢!【参考方案9】:

你不能通过 nib 改变 UIProgressView 的高度。 如果你想改变高度,那么你必须通过自定义的draw方法来实现它。

【讨论】:

应该有更简单的方法! 这个问题很相似,并且有一个基于故事板的解决方案。我们正在使用它,但我确实收到了警告。但是它确实有效。我还没有使用建议的转换,但我更喜欢这种方法。 ***.com/questions/18717895/… 我可以在 xcode 8.3 中很好地改变它,所以也许他们改变了它,所以你现在可以?【参考方案10】:

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

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

【讨论】:

【参考方案11】:

这里是解决方案

你可以使用变换,但问题是 -> 当你改变设备的方向时,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.

谢谢

【讨论】:

在 Xcode 8.3.3、iOS 10 上对我不起作用,进度条仅显示小高度和进度图像的裁剪视图【参考方案12】:

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

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

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

【讨论】:

【参考方案13】:

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

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

现在要更改高度,请将 IBOutlet 添加到 UIViewController 子类中,如下所示:

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

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

self.progressHeight.constant = 9;

这应该很适合你。

【讨论】:

我们不能只在情节提要本身中做到这一点,我的意思是我们可以从情节提要本身更改高度约束的值,为什么我们需要一个 IBOutlet?我很困惑 在情节提要中这样做只会给出警告。 我反对警告。【参考方案14】:

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

class FatProgressView: UIProgressView 

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


【讨论】:

我试过这个,但它弄乱了圆角。就好像整个东西被均匀地向上拉伸,没有保留圆角。【参考方案15】:

这是一个允许设置高度的第三方进度条。 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]];

【讨论】:

【参考方案16】:

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

代码只是

@interface UIProgressView (heavyView)
@end

@implementation UIProgressView (heavyView)
- (CGSize)sizeThatFits:(CGSize)size

    CGSize newSize = CGSizeMake(size.width, 9);
    return newSize;

@end

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

【讨论】:

【参考方案17】:

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

【讨论】:

【参考方案18】:

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

【讨论】:

【参考方案19】:

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

【讨论】:

【参考方案20】:

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

在 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
    

【讨论】:

【参考方案21】:

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

private var _progress: UIProgressView!

    // ...
    _vstack.translatesAutoresizingMaskIntoConstraints = false

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

【讨论】:

【参考方案22】:

我只是在寻找同样的问题,并通过子类化 UIProgressView 并覆盖 sizeThatFits 方法使其工作,就像一个魅力。

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

【讨论】:

【参考方案23】:

简单。斯威夫特 4.

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

【讨论】:

Amit Jagesha 答案的副本。 self.view.frame.width 不利于理解 :)

以上是关于如何增加 UIProgressView 的高度的主要内容,如果未能解决你的问题,请参考以下文章

UIProgressView 高度

iOS 7.1 中以编程方式创建的 UIProgressView 的高度被忽略(始终为 2)

Swift - UIProgressView 对角填充

如何使用 CGAffine 正确缩小 UIProgressView?

如何在 UIProgressView 中设置进度色调颜色

加载 UIWebView 时如何使用 UIProgressView?