Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize UISlider (track image height)

I'm customizing a UISlider. I could set a custom thumb image that is higher than the usual thumb however I could not make the track higher when setting a higher minimum track image but the track height remained the same. It should be possible as in the iPod/Music App on the iPad the volume slider is also higher as the usual slider as you can see here:


(source: cocoia.com)

like image 461
user997128 Avatar asked Nov 04 '11 22:11

user997128


2 Answers

For those that would like to see some working code for changing the track size.

class CustomUISlider : UISlider
{        
    override func trackRectForBounds(bounds: CGRect) -> CGRect {
        //keeps original origin and width, changes height, you get the idea
        let customBounds = CGRect(origin: bounds.origin, size: CGSize(width: bounds.size.width, height: 5.0))
        super.trackRectForBounds(customBounds)
        return customBounds
    }

    //while we are here, why not change the image here as well? (bonus material)
    override func awakeFromNib() {
        self.setThumbImage(UIImage(named: "customThumb"), forState: .Normal)
        super.awakeFromNib()
    }
}

Only thing left is changing the class inside the storyboard:

storyboardstuff

You can keep using your seekbar action and outlet to the object type UISlider, unless you want to add some more custom stuff to your slider.

like image 172
CularBytes Avatar answered Oct 25 '22 12:10

CularBytes


You need to subclass the slider and override the trackRectForBounds: method, like this:

- (CGRect)trackRectForBounds:(CGRect)bounds
{
    return bounds;
}
like image 40
antalkerekes Avatar answered Oct 25 '22 12:10

antalkerekes