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

标签 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]];

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

+ (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;
}

最佳答案

设置UIProgressViewStylebar

Objective-C :

[progressView setProgressViewStyle: UIProgressViewStyleBar];

swift :

progressView.progressViewStyle = .bar

关于ios - 创建方形而不是圆形边缘效果 UIProgressView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26238840/

相关文章:

ios - 模仿 UIBarButtonItem 的按钮

ios - iPhone应用 map 动画

ios - 如何在iOS应用程序上播放的吉他音频样本中应用诸如潜水(音高转换)或哇哇音(压缩)之类的吉他效果?

ios - undefined symbol : ___darwin_check_fd_set_overflow

Swift 3 如何使用 Vapor 发送多部分发布请求

ios - 如何快速创建和子类化仅显示日期的自定义 UIPickerView?

ios - AFNetworking:我的类 NSURLResponse 中的 setRedirectResponseBlock 总是返回空值

objective-c - 重新启动应用程序时数据丢失(核心数据)

ios - Objective-C - 设备无法识别的日期格式

ios - Xcode:开发者帐户转移后更新应用程序