创建正方形而不是圆角边效果UIProgressView

时间:2014-10-07 14:49:54

标签: ios objective-c xcode uiview 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;
}

3 个答案:

答案 0 :(得分: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

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

答案 1 :(得分:1)

UIProgressViewStyle设为bar

<强>目的-C:

[progressView setProgressViewStyle: UIProgressViewStyleBar];

<强>夫特:

progressView.progressViewStyle = .bar

答案 2 :(得分:0)

使用progressViewStyle = .bar

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