Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Change height of UISlider

I'm trying to change the height of a UISlider. This is what I am currently doing:

class CustomSlider: UISlider {

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

@IBInspectable var thumbImage: UIImage? {
    didSet {
        setThumbImage(thumbImage, for: .normal)
    }
}

@IBInspectable var thumbHighlightedImage: UIImage? {
    didSet {
        setThumbImage(thumbImage, for: .highlighted)
    }
}

}

It seems to work for the most part, but I run into an issue that when the slider gets to the end, it is no longer rounded as you can see in the following image:

slider issue

Is there any way to resolve this? I'd prefer to stick with the system slider and not a custom one.

Edit: Changed image.

like image 434
Josh Avatar asked Aug 13 '18 19:08

Josh


1 Answers

Swift 4.2

import UIKit
@IBDesignable open class DesignableSlider: UISlider {

    @IBInspectable var trackHeight: CGFloat = 5

    @IBInspectable var roundImage: UIImage? {
        didSet{
            setThumbImage(roundImage, for: .normal)
        }
    }
    @IBInspectable var roundHighlightedImage: UIImage? {
        didSet{
            setThumbImage(roundHighlightedImage, for: .highlighted)
        }
    }
    override open func trackRect(forBounds bounds: CGRect) -> CGRect {
        //set your bounds here
        return CGRect(origin: bounds.origin, size: CGSize(width: bounds.width, height: trackHeight))
    }
}

*Don't forget to clean your project

like image 144
Sreekanth G Avatar answered Oct 21 '22 00:10

Sreekanth G