在工作中用到了UISlider 控件,UI设计师对滑块设计了新的样式,但是我用常规的设置图片方法,发现滑块不能滑动到头,起始位置也滑不到头,如下图:
解决此问题,需要创建一个UISlider 的子类,然后重写下面方法
-(CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value{
rect.origin.x= rect.origin.x-10;
rect.size.width= rect.size.width+20;
return CGRectInset([super thumbRectForBounds:bounds trackRect:rect value:value],10,10);
}
UISlider 初始化设置如下:
UISlider slider = [[UISlider alloc] initWithFrame:CGRectZero];
[slider setMinimumTrackTintColor:[UIColor ma_colorWithHexString:@"#FFC23F"]];
[slider setMaximumTrackTintColor:[UIColor ma_colorWithHexString:@"#F2F2F2"]];
[slider setMinimumValue:0.0];
[slider setThumbImage:[UIImage imageNamed:@"Slider"] forState:UIControlStateNormal];
[slider addTarget:selfaction:@selector(sliderValueChange:) //selecttor 自定义实现
forControlEvents:UIControlEventValueChanged];
[slider addTarget:selfaction:@selector(sliderDidValueChange:)//selecttor 自定义实现
forControlEvents:UIControlEventTouchUpInside];