UISlider 轨道没有增加厚度
Posted
技术标签:
【中文标题】UISlider 轨道没有增加厚度【英文标题】:UISlider track not increasing in thickness 【发布时间】:2018-08-06 04:18:56 【问题描述】:我似乎无法增加轨道的厚度。一直在尝试其他建议并在文档中寻找此选项,但它似乎不起作用,有人知道为什么吗?:(
class factionButton: UISlider
var factionSlider = UISlider()
func factionBalanceSlider()
factionSlider.frame = CGRect(x: 15, y: 542, width: 386, height: 57)
factionSlider.minimumValueImage = #imageLiteral(resourceName: "Alliance Slider")
factionSlider.maximumValueImage = #imageLiteral(resourceName: "Horde Slider")
factionSlider.setThumbImage(#imageLiteral(resourceName: "Thumb Image"), for: .normal)
factionSlider.minimumTrackTintColor = UIColor(red:0.08, green:0.33, blue:0.69, alpha:0.8)
factionSlider.maximumTrackTintColor = UIColor(red:1.00, green:0.00, blue:0.00, alpha:0.59)
factionSlider.setValue(0.5, animated: true)
factionSlider.isContinuous = true
factionSlider.addTarget(self, action: #selector(recordFactionBalance(sender:)) , for: .valueChanged)
func getSlider() -> UISlider
return factionSlider
override func trackRect(forBounds bounds: CGRect) -> CGRect
let customBounds = CGRect(x: 16, y: 21, width: 343, height: 7)
super.trackRect(forBounds: customBounds)
return customBounds
【问题讨论】:
我认为这里的厚度是指高度? @iosGeek 是的,那个 我想这就是你要找的东西***.com/a/35820464/6080920你提供的高度只有 7 它太小了 @iOSGeek 我的意思是在我丢失它之后再次找到它。谢谢!我输入了 7,然后输入了 700,但它似乎根本没有改变轨道大小:( 【参考方案1】:正如许多其他answers 中提到的,您可以通过创建自定义滑块来更改高度,如下所示,
class CustomSlider: UISlider
override func trackRect(forBounds bounds: CGRect) -> CGRect
var rect = super.trackRect(forBounds: bounds)
rect.size.height = 7
return rect
但在您的特定情况下,您看不到更改,因为您的实现不允许factionSlider
使用overridden
trackRect
。要使用它,您需要将其更改为CustomSlider
,如下所示,
class FactionButton: UISlider
var factionSlider = CustomSlider()
func factionBalanceSlider()
factionSlider.frame = CGRect(x: 15, y: 542, width: 386, height: 57)
factionSlider.minimumValueImage = #imageLiteral(resourceName: "Alliance Slider")
factionSlider.maximumValueImage = #imageLiteral(resourceName: "Horde Slider")
factionSlider.setThumbImage(#imageLiteral(resourceName: "Thumb Image"), for: .normal)
factionSlider.minimumTrackTintColor = UIColor(red:0.08, green:0.33, blue:0.69, alpha:0.8)
factionSlider.maximumTrackTintColor = UIColor(red:1.00, green:0.00, blue:0.00, alpha:0.59)
factionSlider.setValue(0.5, animated: true)
factionSlider.isContinuous = true
factionSlider.addTarget(self, action: #selector(recordFactionBalance(sender:)) , for: .valueChanged)
func getSlider() -> UISlider
return factionSlider
注意在Swift
,class
名称应该以大写开头,正如我在上面更新的那样。其次,我认为FactionButton
不应该是subclass
的UISlider
。
【讨论】:
天哪,谢谢!我不敢相信我错过了这个。是时候睡觉了。谢谢你:3 解决了我所有的问题。谢谢!【参考方案2】:您应该首先从超类中获取当前边界,然后只需更改高度:
override func trackRect(forBounds bounds: CGRect) -> CGRect
var customBounds = super.trackRect(forBounds: bounds)
customBounds.size.height = 7
return customBounds
【讨论】:
我想知道怎么做,谢谢!但赛道似乎仍然和我覆盖它之前一样:( 确保您使用 UISlider 类factionButton
作为情节提要中的 Custom Class
属性。【参考方案3】:
设置矩形大小只会将滑块扩展到底部。所以应该重新计算原点以保持滑块居中。
@IBDesignable
class CustomSlider: UISlider
@IBInspectable var trackHeight: CGFloat = 6
override func trackRect(forBounds bounds: CGRect) -> CGRect
var rect = super.trackRect(forBounds: bounds)
rect.size.height = trackHeight
rect.origin.y -= trackHeight / 2
return rect
【讨论】:
【参考方案4】:我添加了这个
1.
class CustomSlider: UISlider
override func trackRect(forBounds bounds: CGRect) -> CGRect
let point = CGPoint(x: bounds.minX, y: bounds.midY)
return CGRect(origin: point, size: CGSize(width: bounds.width, height: 10)) //this height is the thickness
-
故事板 - 将 UISlider 类更改为我的 CustomSlider
仅供像我这样的新手.. 改变颜色在这里:)
【讨论】:
以上是关于UISlider 轨道没有增加厚度的主要内容,如果未能解决你的问题,请参考以下文章