我正在UIProgressView
从nib
. 我想增加它的高度,但它固定为 9。因为iPad
我需要增加它的高度。怎么做?
23 回答
用 CGAffineTransform 改变尺寸:
CGAffineTransform transform = CGAffineTransformMakeScale(1.0f, 3.0f);
progressView.transform = transform;
使用布局约束对我有用:
放置这个来源
@implementation UIProgressView (customView)
- (CGSize)sizeThatFits:(CGSize)size {
CGSize newSize = CGSizeMake(self.frame.size.width, 9);
return newSize;
}
@end
初始化后,只需在代码中设置 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];
有一种简单的方法可以在 Interface Builder 中更改高度。无需代码,您的更改将显示在 IB 中。
在 Storyboard 或 xib 中选择 UIProgressView 对象,选择 Editor > Pin > Height。这将创建一个高度约束,并允许您在最左侧(Utilizes)面板的 Attributes Inspector 中更改其值。
以下代码适用于最新的 swift xCode:
var transform : CGAffineTransform = CGAffineTransformMakeScale(1.0, 6.0)
progressView.transform = transform
斯威夫特 3:
progressView.transform = progressView.transform.scaledBy(x: 1, y: 9)
斯威夫特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
}
}
您不能通过 nib 更改 UIProgressView 的高度。如果要更改高度,则必须通过自定义绘制方法来实现。
您还可以使用 AutoLayout 来实现相同的外观,它适用于 iOS 7.1。只需添加一个高度约束,该约束等于您希望进度条的高度。有关更多详细信息,请查看此类似问题的答案。
这是解决方案
您可以使用变换,但出现的问题是 -> 当您更改设备的方向时,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.
谢谢
对于 iOS 7+,使用 Core Graphics 和CATransform3DScale在该视图中缩放图层:
CATransform3D transform = CATransform3DScale(progressView.layer.transform, 1.0f, 3.0f, 1.0f);
progressView.layer.transform = transform;
只需确保在设置progressView 的框架之后执行此操作,而不是之前。
对于 iOS 7 及更高版本,我执行了以下操作(a)有效且(b)不生成警告:
首先UIProgressView
在情节提要中添加到我的视图控制器。UIProgressView
然后通过 CTRL+拖动UIProgressView
到自身添加一个高度约束。将使用默认值创建约束2
。别管那个。
现在要更改高度,请在代码中添加一个IBOutlet
子UIViewController
类,如下所示:
@property (nonatomic, weak) IBOutlet NSLayoutConstraint *progressHeight;
然后在您的代码中,可能- viewDidLoad
添加:
self.progressHeight.constant = 9;
这应该很适合你。
只需子类化并调整内在大小:
class FatProgressView: UIProgressView {
override var intrinsicContentSize: CGSize {
return CGSize(width: UIView.noIntrinsicMetric, height: 20.0)
}
}
这是允许设置高度的第三方进度条。 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]];
您可以实现一个类别(新文件 - 类别),只需在类的开头添加类别。它也适用于 iboutlet(笔尖/故事板)。
代码只是
@interface UIProgressView (heavyView)
@end
@implementation UIProgressView (heavyView)
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize newSize = CGSizeMake(size.width, 9);
return newSize;
}
@end
如果您只想对一个progressView 应用更改并且您的班级中有多个progressView,您可以只使用一个子类。
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));
}]];
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)]];
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
}
}
或者,可以以编程方式实现布局高度。
private var _progress: UIProgressView!
// ...
_vstack.translatesAutoresizingMaskIntoConstraints = false
let progressHeightAnchor = _progress.heightAnchor
.constraint(equalToConstant: 16.0)
NSLayoutConstraint.activate([progressHeightAnchor /* , ... */ ])
我只是在寻找同样的问题,并让它与子类化UIProgressView
和覆盖sizeThatFits
方法一起工作,就像一个魅力。
class BigProgressView: UIProgressView {
override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: size.width, height: 5)
}
}
简单的。斯威夫特 4。
progressBar.transform = CGAffineTransform(scaleX: self.view.frame.width / progressBar.frame.width, y: self.view.frame.height / progressBar.frame.height)