我正在尝试更改UISlider的高度。这是我目前正在做的事情:
class CustomSlider: UISlider {
override func trackRect(forBounds bounds: CGRect) -> CGRect {
var newBounds = super.trackRect(forBounds: bounds)
newBounds.size.height = 20.0
return newBounds
}
@IBInspectable var thumbImage: UIImage? {
didSet {
setThumbImage(thumbImage, for: .normal)
}
}
@IBInspectable var thumbHighlightedImage: UIImage? {
didSet {
setThumbImage(thumbImage, for: .highlighted)
}
}
}
似乎在大多数情况下都可以使用,但是我遇到一个问题,即当滑块移到末尾时,就不再舍入,如下图所示:
有什么办法解决这个问题?我希望坚持使用系统滑块,而不是自定义滑块。
编辑:更改的图像。
答案 0 :(得分:3)
Swift 4.2
import UIKit
@IBDesignable open class DesignableSlider: UISlider {
@IBInspectable var trackHeight: CGFloat = 5
@IBInspectable var roundImage: UIImage? {
didSet{
setThumbImage(roundImage, for: .normal)
}
}
@IBInspectable var roundHighlightedImage: UIImage? {
didSet{
setThumbImage(roundHighlightedImage, for: .highlighted)
}
}
override open func trackRect(forBounds bounds: CGRect) -> CGRect {
//set your bounds here
return CGRect(origin: bounds.origin, size: CGSize(width: bounds.width, height: trackHeight))
}
}