简体   繁体   English

如何增加 UIProgressView 的高度

[英]How to increase height of UIProgressView

I am creating UIProgressView from nib .我正在从nib创建UIProgressView I want to increase its height but it is fixed to 9. For iPad I need to increase its height.我想增加它的高度,但它固定为 9。对于iPad我需要增加它的高度。 How it can be done?怎么做?

Use CGAffineTransform to change dimensions:使用 CGAffineTransform 更改维度:

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

Using layout constraints worked for me:使用布局约束对我有用:

在此处输入图片说明

place this source放置此源

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

Just set the frame of the UIProgressView in code after it has been initialised.只需在初始化后在代码中设置 UIProgressView 的框架。 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.这在 iOS 5 上对我有用。不过我正在使用自定义 trackImage 和 progressImage。 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 添加到另一个视图并希望它与包含视图的大小相同,因此将框架设置为 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];

There is a simple way to change the height within Interface Builder.有一种简单的方法可以在 Interface Builder 中更改高度。 No code required and your change will show in IB.无需代码,您的更改将显示在 IB 中。

Select the UIProgressView object in your storyboard or xib, choose Editor > Pin > Height.在故事板或 xib 中选择 UIProgressView 对象,选择 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.这将创建一个高度约束,并允许您在最左侧(Utilizes)面板的 Attributes Inspector 中更改其值。

The following code works on the newest swift xCode:以下代码适用于最新的 swift xCode:

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

斯威夫特 3:

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

Swift3斯威夫特3

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:如果您正在使用IBDesignable类,则可以从故事板中使用以下内容对其进行调整:

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

You can not change the height of UIProgressView through nib.您不能通过笔尖更改 UIProgressView 的高度。 If you want to change the height then you have to implement it through custom draw method.如果要更改高度,则必须通过自定义绘制方法来实现。

Here is the Solution这是解决方案

You can use transform, but problem arises that -> when you change orientation of the device, UIProgressView height becomes original one.您可以使用转换,但出现问题 -> 当您更改设备的方向时, UIProgressView 高度变为原始高度。

So best way to increase UIProgressView height is所以增加 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.

Thank you谢谢

You can also use AutoLayout to achieve the same look and it works in iOS 7.1.您还可以使用 AutoLayout 来实现相同的外观,它适用于 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 https://stackoverflow.com/a/19606981/142358

For iOS 7+, use Core Graphics and CATransform3DScale to scale the layer in that view:对于 iOS 7+,使用 Core Graphics 和CATransform3DScale在该视图中缩放图层:

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.只需确保设置 progressView 框架之后执行此操作,而不是之前。

For iOS 7 and above, I did the following which (a) works and (b) does not generate a warning:对于 iOS 7 及更高版本,我执行了以下操作,其中 (a) 有效且 (b) 不会生成警告:

First add UIProgressView to my View Controller in the storyboard.首先将UIProgressView添加到我的故事板中的视图控制器。 Then add a Height constraint to the UIProgressView by CTRL+Dragging the UIProgressView to itself.然后通过 CTRL+拖动UIProgressViewUIProgressView添加高度约束。 The constraint will be created with a default value of 2 .将使用默认值2创建约束。 Leave that alone.别管它。

Now to change the height add an IBOutlet to the UIViewController subclass in code like this:现在要更改高度,在UIViewController子类中添加一个IBOutlet ,代码如下:

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

Then in your code, probably - viewDidLoad , add:然后在您的代码中,可能- viewDidLoad ,添加:

self.progressHeight.constant = 9;

This should work out nicely for you.这应该很适合你。

Just subclass and adjust the intrinsic size:只需子类化并调整固有大小:

class FatProgressView: UIProgressView {

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

}

Here is a third party progress bar that allows setting height.这是允许设置高度的第三方进度条。 https://github.com/yannickl/YLProgressBar https://github.com/yannickl/YLProgressBar

在此处输入图片说明

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

在此处输入图片说明

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).它也适用于 iboutlet(笔尖/故事板)。

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.如果您只想对一个 progressView 应用更改,并且您的类中有多个 progressView,则可以只使用子类。

Mayur's method worked well for me in iOS 7, here's my code (uses UIImage+BBlock) 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));
    }]];

Instead of using interface builder, the height of UIProgressView can be changed by adding constraints to it programmatically.可以通过以编程方式向UIProgressView添加约束来更改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)]];

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.您应该立即看到进度视图调整大小以适应其容器的边界。

调整大小的 UIProgressView

We can set the height of UIProgressView by creating the custom class of ProgressView by subclassing the UIProgressView .我们可以设置的高度UIProgressView由通过继承创建自定义类ProgressView的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
    }
}

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

I was just looking for the same problem and got it working with just subclassing UIProgressView and overriding sizeThatFits method, works like a charm.我只是在寻找同样的问题,并通过UIProgressView和覆盖sizeThatFits方法让它工作,就像一个魅力。

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

Simple.简单的。 Swift 4.斯威夫特 4.

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

I found this website, it may be helpful for newer swift version.我找到了这个网站,它可能对更新的 swift 版本有帮助。 https://blckbirds.com/post/progress-bars-in-swiftui/ Custom Progress Bar https://blckbirds.com/post/progress-bars-in-swiftui/自定义进度条

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM