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

jac*_*300 4 xcode objective-c uiview uiprogressview ios

我有一个 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]];
Run Code Online (Sandbox Code Playgroud)

//上面用于在进度视图中创建图像的方法定义

+ (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;
}
Run Code Online (Sandbox Code Playgroud)

Lal*_*hna 5

UIProgressViewStyle设置为bar

目标-C:

[progressView setProgressViewStyle: UIProgressViewStyleBar];
Run Code Online (Sandbox Code Playgroud)

迅速:

progressView.progressViewStyle = .bar
Run Code Online (Sandbox Code Playgroud)