Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read size of UISlider thumb image

I'm trying to position an additional UIView centered above the thumb in a UISlider. To do this, I need the width of the thumb image. In iOS6, this works fine. I can use:

CGFloat thumbWidth = self.navSlider.currentThumbImage.size.width;

(As seen in this answer: How to get the center of the thumb image of UISlider)

This returns 0.0f in iOS7. I've also tried reading it using:

UIImage *thumb = [self.navSlider thumbImageForState:UIControlStateNormal];

But thumb ends up nil.

Is it possible to read the size of the default slider thumb image? Or will I have to find it, set a constant, and how Apple doesn't change it later?

like image 563
Pj Dietz Avatar asked Jan 23 '14 16:01

Pj Dietz


2 Answers

The docs on the currentThumbImage property say:

If no custom thumb images have been set using the setThumbImage:forState: method, this property contains the value nil. In that situation, the receiver uses the default thumb image for drawing.

The docs on thumbImageForState: are less clear:

Return Value The thumb image associated with the specified state, or nil if an appropriate image could not be retrieved.

I think you might be out of luck trying to figure out the default thumb size. How about installing a "custom" thumb image that looks exactly like the system image? That would solve the problem of Apple changing it out from under you.

like image 64
Duncan C Avatar answered Oct 11 '22 10:10

Duncan C


I use this in my subclass:

CGRect trackRect = [self trackRectForBounds:self.bounds];
CGRect thumbRect = [self thumbRectForBounds:self.bounds trackRect:trackRect value:0];
CGSize thumbSize = thumbRect.size;
like image 35
Rudolf Adamkovič Avatar answered Oct 11 '22 09:10

Rudolf Adamkovič