我创建了一个具有以下属性的UIProgressView
progressView.progressTintColor = UIColor.appChallengeColorWithAlpha(1.0)
progressView.trackTintColor = UIColor.clearColor()
progressView.clipsToBounds = true
progressView.layer.cornerRadius = 5我使用UIView作为边框。似乎他的进步= 1,这正是我想要的方式。

但是,如果进度值小于1,则角不像应该的那样四舍五入。

我漏掉了什么吗?我怎样才能把它变成圆角?
发布于 2016-12-29 05:18:56
在搜索和尝试之后,我决定创建自己的自定义进度视图。这是任何可能在相同问题上发现他们的人的代码。
import Foundation
import UIKit
class CustomHorizontalProgressView: UIView {
var progress: CGFloat = 0.0 {
didSet {
setProgress()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
self.backgroundColor = UIColor.clearColor()
}
override func drawRect(rect: CGRect) {
super.drawRect(rect)
setProgress()
}
func setProgress() {
var progress = self.progress
progress = progress > 1.0 ? progress / 100 : progress
self.layer.cornerRadius = CGRectGetHeight(self.frame) / 2.0
self.layer.borderColor = UIColor.grayColor().CGColor
self.layer.borderWidth = 1.0
let margin: CGFloat = 6.0
var width = (CGRectGetWidth(self.frame) - margin) * progress
let height = CGRectGetHeight(self.frame) - margin
if (width < height) {
width = height
}
let pathRef = UIBezierPath(roundedRect: CGRect(x: margin / 2.0, y: margin / 2.0, width: width, height: height), cornerRadius: height / 2.0)
UIColor.redColor().setFill()
pathRef.fill()
UIColor.clearColor().setStroke()
pathRef.stroke()
pathRef.closePath()
self.setNeedsDisplay()
}
}只需将上面的代码放入一个快速文件中,然后将一个UIView拖放到IB中,并给它CustomHorizontalProgressView类。仅此而已。
发布于 2017-03-20 03:11:19
UIProgressView有两个部分,进度部分和跟踪部分。如果您使用显示,您可以看到它只有两个子视图。进度视图层次结构非常简单。所以..。
目标-C
- (void)layoutSubviews
{
[super layoutSubviews];
[self.progressView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.layer.masksToBounds = YES;
obj.layer.cornerRadius = kProgressViewHeight / 2.0;
}];
}Swift (3,4和5+)
override func layoutSubviews() {
super.layoutSubviews()
subviews.forEach { subview in
subview.layer.masksToBounds = true
subview.layer.cornerRadius = kProgressViewHeight / 2.0
}
}我承认subclass或extend progressView是推荐的方法。如果你不想为了这么简单的效果而这么做,这可能会起作用。如果苹果会改变视图的层次结构,那么某些事情可能会出错。
发布于 2018-04-11 08:46:39
就这么做吧
layer.cornerRadius = *desired_corner_radius*
clipsToBounds = truehttps://stackoverflow.com/questions/40925453
复制相似问题