2014-10-07 82 views
2

我有一个UIProgressView,我试图让它具有正方形而不是圆形边缘。我没有看到任何明显的方式来做到这一点,所以我的想法是让进度图像本身比方框更大一点,以便进度图像裁剪成方形。但是,无论如何改变进度图像的大小,我似乎都无法实现这一效果。任何人都可以告诉我是否有办法实现我的目标?创建正方形而不是圆角边缘效果UIProgressView

这里是我现在有一个UIView子类中:

self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; 
self.progressView.trackImage = [PLStyle imageForColor:[PLColors greyLight] inFrame:CGRectMake(0, 0, 1, ProgressViewHeight)]; 
self.progressView.progressImage = [PLStyle imageForColor:[PLColors orange] inFrame:CGRectMake(0, 0, 1, ProgressViewHeight)]; 

[self.progressView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[self addSubview:self.progressView]; 

[self addConstraint: 
[NSLayoutConstraint constraintWithItem:self.progressView 
           attribute:NSLayoutAttributeLeading 
           relatedBy:NSLayoutRelationEqual 
           toItem:self.imageView 
           attribute:NSLayoutAttributeTrailing 
          multiplier:1 
           constant:LeadingOrTrailingSpace]]; 

[self addConstraint: 
[NSLayoutConstraint constraintWithItem:self.progressView 
           attribute:NSLayoutAttributeCenterY 
           relatedBy:NSLayoutRelationEqual 
           toItem:self 
           attribute:NSLayoutAttributeCenterY 
          multiplier:1 
           constant:0]]; 

[self addConstraint: 
[NSLayoutConstraint constraintWithItem:self.progressView 
           attribute:NSLayoutAttributeTrailing 
           relatedBy:NSLayoutRelationEqual 
           toItem:self.counter 
           attribute:NSLayoutAttributeLeading 
          multiplier:1 
           constant:-LeadingOrTrailingSpace]]; 

[self addConstraint: 
[NSLayoutConstraint constraintWithItem:self.progressView 
           attribute:NSLayoutAttributeHeight 
           relatedBy:NSLayoutRelationEqual 
           toItem:nil 
           attribute:NSLayoutAttributeNotAnAttribute 
          multiplier:1 
           constant:ProgressViewHeight]]; 

//上述用于在进度视图

+ (UIImage *)imageForColor:(UIColor *)color inFrame:(CGRect)rect 
{ 
    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetFillColorWithColor(context, [color CGColor]); 
    CGContextFillRect(context, rect); 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return image; 
} 

回答

2

你总是可以推出自己创建图像的方法定义很容易。

@interface MyProgressView : UIView 
@property (assign) float progress; 
@end 

@implementation MyProgressView { 
    UIView *progressBar; 
} 
- (void)setProgress:(float)progress { 
    _progress = progress; 
    [self setNeedsLayout]; 
} 
- (void)layoutSubviews { 
    CGRect frame = self.bounds; 
    frame.size.width *= progress; 
    progressBar.frame = frame; 
} 
@end 

您也可以使用“最小/最大值”+“设定值”,并进行计算,如果更适合您的应用程序。如果您不喜欢一起粉碎一堆UIView以做简单的矩形绘图,您也可以使用CALayer

0

使用progressViewStyle =的.bar

let p = UIProgressView(frame: .zero) 
p.progressViewStyle = .bar 
1

设置UIProgressViewStylebar

的Objective-C:

[progressView setProgressViewStyle: UIProgressViewStyleBar]; 

斯威夫特:

progressView.progressViewStyle = .bar