5

I can't seem to increase the thickness of the track. Been trying other recommendations and looking for this option in the documentation but it doesn't seem to be working, anyone know why?:(

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
}
HayashiEsme
  • 303
  • 4
  • 9

4 Answers4

13

As mentioned in many other answers, you can change the height by creating a custom slider as below,

class CustomSlider: UISlider {

    override func trackRect(forBounds bounds: CGRect) -> CGRect {
        var rect = super.trackRect(forBounds: bounds)
        rect.size.height = 7
        return rect
    }
}

But in your particular case, you are not seeing the change because your implementation is not allowing the factionSlider to use overridden trackRect. To use that you need to change that to CustomSlider as below,

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
    }
}

Note In Swift, class name should start with Capital as i updated above. Secondly, I think FactionButton should not be a subclass of UISlider.

Kamran
  • 14,987
  • 4
  • 33
  • 51
4

You should get the current bounds from the super class first, then just change the height:

override func trackRect(forBounds bounds: CGRect) -> CGRect {
    var customBounds = super.trackRect(forBounds: bounds)
    customBounds.size.height = 7
    return customBounds
}
RLoniello
  • 2,309
  • 2
  • 19
  • 26
  • I was going to figure out how to do this, thanks! But the track seems to still stay the same as before I overrode it:( – HayashiEsme Aug 06 '18 at 04:32
  • make sure you are using your UISlider class `factionButton` for the `Custom Class` Attribute in your storyboard. – RLoniello Aug 06 '18 at 04:53
2

Setting the rect size expands the slider to the bottom only. So the origin should be recalculated to keep the slider centered.

@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
  }
}
contrapost
  • 673
  • 12
  • 22
3ps
  • 109
  • 5
0

enter image description here

I made this by adding this

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 
    }
    
}
  1. storyboard - change UISlider class to my CustomSlider

enter image description here

FYI for newbie like me.. change color is here :)

enter image description here

Energy
  • 940
  • 13
  • 20