简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM