iOS - iPhone : UISlider track image disappearing randomly

余生颓废 提交于 2019-12-23 11:59:09

问题


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?


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/9317366/ios-iphone-uislider-track-image-disappearing-randomly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!