Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISlider track not increasing in thickness

Tags:

swift

uislider

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
}
like image 985
HayashiEsme Avatar asked Aug 06 '18 04:08

HayashiEsme


3 Answers

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.

like image 171
Kamran Avatar answered Oct 17 '22 19:10

Kamran


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
}
like image 28
RLoniello Avatar answered Oct 17 '22 21:10

RLoniello


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
  }
}
like image 39
3ps Avatar answered Oct 17 '22 20:10

3ps