class CustomUISlider : UISlider {
override func trackRect(forBounds bounds: CGRect) -> CGRect {
//keeps original origin and width, changes height, you get the idea
let customBounds = CGRect(origin: bounds.origin, size: CGSize(width: bounds.size.width, height: 5.0))
super.trackRect(forBounds: customBounds)
return customBounds
}
//while we are here, why not change the image here as well? (bonus material)
override func awakeFromNib() {
self.setThumbImage(UIImage(named: "customThumb"), for: .normal)
super.awakeFromNib()
}
}
5 回答
这是我最近的实施,基于CularBytes的......
通过设置自定义类
在故事板中的UISlider上使用它
IBInspectable允许您从故事板设置高度
接受的答案将使用
minimumValueImage
和maximumValueImage
不合需要地更改滑块's width in some cases, like if you' . 如果您只想更改高度并保留其他所有内容,请使用以下代码:对于那些希望看到一些改变轨道大小的工作代码的人 .
唯一剩下的就是更改故事板中的类:
您可以继续使用搜索栏操作和插座到对象类型UISlider,除非您想在滑块上添加更多自定义内容 .
我找到了我要找的东西 . 以下方法只需要在子类中进行编辑 .
你可以玩这个,看看会发生什么: