UISlider does not redraw automatically

我只是一个虾纸丫 提交于 2019-12-02 18:52:09

问题


I've an UISlider on my app and I need sometimes to update not only its value but also its minimumValue. The values are changed but if I call the setValue method or assign a new value for my slider, it has the new value but the slider does not repaint itself to the position it should be for this new value. How can I repaint it? This is a piece of code where I change the minimumValue.

if([array count] < 50)
    [sliderTamanyo setMinimumValue:2];
else if([array count] < 200)
    [sliderTamanyo setMinimumValue:3];
else
    [sliderTamanyo setMinimumValue:4];
if(sliderTamanyo.value < sliderTamanyo.minimumValue)
     self.sliderTamanyo.value = self.sliderTamanyo.minimumValue;     
[self.sliderTamanyo setNeedsDisplay];                

回答1:


As UISlider inherits from UIView can you not just call [yourSlider setNeedsDisplay]; after you've updated the values.

This will force the UISlider instance to completely redraw itself and may therefore take into account the new values you've set.

EDIT:

There seems to be some common issues when embedding a UISlider in a UIBarButtonItem - please see this Stack Overflow question for more information.




回答2:


I've solved that by removing and adding again the slider to the toolbar. I've used this code:

NSMutableArray *items = [NSMutableArray arrayWithArray:self.toolbar.items];
id slider = [items lastObject]; 
[items removeLastObject];
self.toolbar.items = items;    
[items addObject:slider];
self.toolbar.items = items;

I've recovered my slider using an id instead an UISlider because that slider is inside a UIBarButtonItem



来源:https://stackoverflow.com/questions/6952754/uislider-does-not-redraw-automatically

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