我为这个问题做了一种解决方法。我希望有人可以为正常的 UIProgressView 给出一个很好的答案。
我写了一个带有圆角的 UIView 子类和一个里面的视图,它根据给定的进度改变它的大小。我只使用颜色作为背景,但图像也可以。这是代码:
#import "CustomProgressView.h"
#import <QuartzCore/QuartzCore.h>
@interface CustomProgressView ()
@property (nonatomic, retain) UIView *progressView;
@end
@implementation CustomProgressView
@synthesize progressColor,trackColor,progressView,progress;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer.cornerRadius = 5;
// clipsToBounds is important to stop the progressView from covering the original view and its round corners
self.clipsToBounds = YES;
self.progressView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, frame.size.height)];
[self addSubview:self.progressView];
}
return self;
}
-(void)setProgressColor:(UIColor *)theProgressColor {
self.progressView.backgroundColor = theProgressColor;
progressColor = theProgressColor;
}
-(void)setTrackColor:(UIColor *)theTrackColor {
self.backgroundColor = theTrackColor;
trackColor = theTrackColor;
}
-(void)setProgress:(float)theProgress {
progress = theProgress;
CGRect theFrame = self.progressView.frame;
theFrame.size.width = self.frame.size.width * theProgress;
self.progressView.frame = theFrame;
}
@end