Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make UISlider thicker when it is first displayed?

I am trying to make a UISlider that has a fat track. I tried this, and it almost works. However, when the UISlider is first displayed, the track bar is normal. Only when it gets a touch event does it get thicker.

Here is the code I added to my subclass of UISlider, as recommended:

- (CGRect)trackRectForBounds:(CGRect)bounds
{
    return bounds;
}

I don't understand how this works, so I can't figure out why it isn't working perfectly. Here are my questions:

  1. How does this work?
  2. How can I control how thick it makes the track?
  3. How can I make it affect the track when the UISlider is first drawn?

Of course, the third question is the most important, though the others will help me in my understanding.

One other thing to consider: when it is first displayed, I hide the thumb:

[slider setThumbImage:[[UIImage alloc] init] forState:UIControlStateNormal];

When the user touches the slider, I display the thumb like this:

[slider setThumbImage:nil forState:UIControlStateNormal];

I mention this because I wonder if that is somehow interfering with the trackRectForBounds magic.

Thanks!

like image 779
Bob Avatar asked Mar 15 '13 23:03

Bob


1 Answers

Question 1. From the docs:

If you want to customize the track rectangle, you can override this method and return a different rectangle. The returned rectangle is used to scale the track and thumb images during drawing.

The default implementation of this method returns a smaller rectangle than the one it is passed. Yours simply returns what it's passed, instead of shrinking it first.

Question 2. Return a larger or smaller CGRect, for example:

return CGRectMake(0, 0, 500, 200);

Question 3. I think the method only gets called if you don't set a custom image, which is why it has no effect until you set the image to nil. If you want to override the image, try overriding the slider images with actual images that look the way you want.

like image 181
Aaron Brager Avatar answered Oct 13 '22 10:10

Aaron Brager