Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - iPhone : UISlider track image disappearing randomly

I have a customized UISlider which has its track image disappear sometimes. It happens at random and when its parent view controller is pushed to visible (I never see it actually disappear).

Here is my code for setting up the UISlider:

timeSlider = [[UISlider alloc] initWithFrame:CGRectMake(55, 8, 210, 23)];
timeSlider.backgroundColor = [UIColor clearColor]; 

UIImage *blutrackImg = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bluetrack" ofType:@"png"]];
UIImage *whitetrackImg = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"whitetrack" ofType:@"png"]];

//UIEdgeInsetsMake(top,left,bottom,right)
UIImage *stetchLeftTrack = [blutrackImg stretchableImageWithLeftCapWidth:9.0 topCapHeight:0.0]; 
UIImage *stetchRightTrack = [whitetrackImg stretchableImageWithLeftCapWidth:9.0 topCapHeight:0.0];
[timeSlider setThumbImage: [UIImage imageNamed:@"whiteslide2.png"] forState:UIControlStateNormal];
[timeSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[timeSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];

timeSlider.continuous = NO;
[timeSlider addTarget:self action:@selector(trackTime:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:timeSlider];

I am building on iOS 5.0. Has anyone ever ran into such a thing?

like image 273
Zigglzworth Avatar asked Feb 16 '12 18:02

Zigglzworth


4 Answers

SOLUTION FOUND: I believe this problem was caused by the following mistake:

I had a timer which was setting the value of the slider using the slider.value property instead of the [slider setValue: animated:] method. I was also not sanity checking the value being set.

Addressing those to issues has solved the problem.

like image 120
Zigglzworth Avatar answered Nov 12 '22 09:11

Zigglzworth


I second the "Answer" and want to esp. point out the bit about sanity checking the values. I don't think it matters if you use slider.value or [slider setValue], setting it to a NaN means the background in iOS5 disappears.

NB: Getting a NaN as a returned value something that can happen often if you're using the iPod library and asking for current position.

like image 42
DreamTimeStudioZ Avatar answered Nov 12 '22 09:11

DreamTimeStudioZ


i had the same problem, it was caused by duplication resources. Check it in Build Phases, if it is rename recources in xCode.

like image 2
Sergio Avatar answered Nov 12 '22 07:11

Sergio


NaN was my issue too, but it's worse than I expected... even setting the UISlider to NAN once, on iOS5, seems to forever break that instance of the control was was surprising and took a little time to track down.

like image 1
IanJ Avatar answered Nov 12 '22 09:11

IanJ