创建方形而非圆角效果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;
}

将UIProgressViewStyle设置为bar

OBJECTIVE-C:

[progressView setProgressViewStyle: UIProgressViewStyleBar];

Swift:

progressView.progressViewStyle = .bar

你总是可以很容易地滚动自己的。

@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

使用progressViewStyle=.bar

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

最新更新