I have my own custom view that acts like a progress bar. Here is the code for it:
class ProgressBar: UIView {
var bottomProgressBar = CAShapeLayer()
var topProgressBar = CAShapeLayer()
override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configure()
}
func configure() {
bottomProgressBar.strokeColor = UIColor.white.cgColor
bottomProgressBar.opacity = 0.1
bottomProgressBar.lineWidth = self.frame.height
bottomProgressBar.strokeStart = 0
bottomProgressBar.strokeEnd = 1
self.layer.addSublayer(bottomProgressBar)
topProgressBar.strokeColor = UIColor.white.cgColor
topProgressBar.lineWidth = self.frame.height
topProgressBar.strokeStart = 0
topProgressBar.strokeEnd = 1
self.layer.addSublayer(topProgressBar)
animate(toValue: 0)
}
override func layoutSubviews() {
super.layoutSubviews()
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: self.frame.width, y: 0))
bottomProgressBar.path = path.cgPath
topProgressBar.path = path.cgPath
}
}
Now, when I add this using storyboards, by adding an UIView inside the controller and setting the Custom Class to ProgressBar I can see it visible when running the app (it should have a white color with alpha 0.1).
But when I'm trying to added programatically , it's not visible at all. Only if I explicitly set the background color:
myProgressBar.backgroudColor = .red
This is not what I want. I want it to have the white color and alpha 0.1 that is the initial value.
Here is my code for adding it programatically:
var progressBar = ProgressBar()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(progressBar)
progressBar.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: progressBar, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: progressBar, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: progressBar, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 100).isActive = true
NSLayoutConstraint(item: progressBar, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 1).isActive = true
}
I don't seem to understand what is the difference between adding it using storyboards and adding it programatically.
I see that the constraints are correct and the view is added, but why doesn't the default color appear ?
Does anyone have an idea what my issue is in this case ?
edit:
Added code for the animate method:
func animate(toValue: Double) {
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.duration = 1.0
animation.fromValue = 0
animation.toValue = toValue
topProgressBar.strokeEnd = CGFloat(toValue)
topProgressBar.animation(forKey: "animate")
}