How to Customise Uislider Height

Customize UISlider Height - Programmatically Created

You'll need to create a custom slider, and override trackRect(forBounds bounds: CGRect) -> CGRect

class CustomSlider: UISlider {
let trackHeight: CGFloat = 12 //desired track width, in points
override func trackRect(forBounds bounds: CGRect) -> CGRect {
let track = super.trackRect(forBounds: bounds)
return CGRect(x: track.origin.x, y: track.origin.y, width: track.width, height: trackHeight)
}
}

and change your implementation to use this

let slider: CustomSlider = {
let slider = CustomSlider()
slider.minimumValue = 1
slider.maximumValue = 7
slider.value = 1
slider.maximumTrackTintColor = UIColor.red
slider.minimumTrackTintColor = UIColor.green
slider.thumbTintColor = UIColor.blue
slider.isContinuous = true
slider.translatesAutoresizingMaskIntoConstraints = false
return slider
}()

Make UISlider height larger?

I found what I was looking for. The following method just needs to be edited in a subclass.

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

UISlider track not increasing in thickness

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.

UISlider track height

found this but its not working

Because you're using it wrong. You don't call it. You subclass, and override it.

However, the right approach is to supply new images for the track, with setMinimumTrackImage and setMaximumTrackImage.

How to change the size of a UISlider thumb when seeking

Create a normal and larger thumb image with the sizes you want as PDF and add it to the .xcassets.

Then use the following lines of code:

let normal = UIImage(named: "thumbSmall")
slider.setThumbImage(normal, for: .normal)
let highlighted = UIImage(named: "thumbLarger")
slider.setThumbImage(highlighted, for: .highlighted)

Then during sliding the larger thumb image is shown.

A short test looks like this:

thumb size during sliging



Related Topics



Leave a reply



Submit