How to make UISlider thicker when it is first displayed?

I am trying to make a UISlider with a thick track. I tried This , and it almost works. However, when UISlider is first displayed, the track bar is normal. Only when he receives a touch event does he become fatter.

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

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

I do not understand how this works, so I can not understand why it does not work perfectly. Here are my questions:

  • How it works?
  • How can I control how thick he makes the track?
  • How can I make it affect the track when UISlider is drawn first?

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

Another thing to keep in mind: when it first appears, I will hide my thumb:

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

When the user touches the slider, I show my thumb as follows:

 [slider setThumbImage:nil forState:UIControlStateNormal]; 

I mention this because I am wondering if this somehow interferes with the magic of trackRectForBounds.

Thanks!

+6
source share
1 answer

Question 1. From the documents :

If you want to customize the rectangle of the track, you can override this method and return another rectangle. The returned rectangle is used to scale tracks and thumb images while drawing.

By default, this method returns a smaller rectangle than the one it passed. Yours simply returns what it passed, instead of squeezing it first.

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

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

Question 3. I think that the method is called only if you do not install a custom image, so it does not work until you set the image to nil . If you want to override the image, try redefining the slider images with actual images that look the way you want.

+4
source

All Articles