in my custom UISlider I want to achieve a rounded corner inner bar. I'm using no thumb. it works fine in iOS 13, but in 14 it crashes with error :
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSSingleObjectArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
Custom UISlider code
class CustomSlider: UISlider {
@IBInspectable var trackHeight: CGFloat = 30
override func layoutSubviews() {
super.layoutSubviews()
self.layer.sublayers![1].cornerRadius = 12
self.thumbTintColor = .clear
}
}
self.layer.sublayers![1].cornerRadius = 12, this line is creating the issue. If I comment this line code works fine.
You can use if let
...
override func layoutSubviews() {
super.layoutSubviews()
if let arrSubLayer = layer.sublayers, arrSubLayer.count > 1 {
arrSubLayer[1].cornerRadius = 12
}
self.thumbTintColor = .clear
}
It will check for the sub layers. If any then it will check the count > 1 as you are trying to trying to access index 1.