UIslider thumb image doesn't start from the beginning

I am trying to implement a control to show the progress of a video, and I am using a UISlider with a custom thumb image, but the thumb image does not start at the beginning and does not reach the end.

playerProgress = UISlider(frame: CGRectMake((btnImage.size.width + 2 * VideoViewControllerUX.ControlPadding), 0, (screenRect.size.width - (btnImage.size.width + 3 * VideoViewControllerUX.ControlPadding)), btnImage.size.height))
playerProgress.setThumbImage(UIImage(named: "slider"), forState: UIControlState.Normal)
playerProgress.maximumValue = 100
playerProgress.minimumValue = 0
playerProgress.addTarget(self, action: "playerProgressChanged:", forControlEvents: UIControlEvents.ValueChanged)

      

Slider Image 1

I'm not sure what's going on.

  • Thumb Image:

    Thumb Image

+6


source to share


2 answers


What you see is normal. The slider leaves extra space at both ends so that the thumb has a minimum or maximum value when the edge of the thumb is at the end of the slider frame.

Take a look at these sliders. They have the same horizontal position and width:

enter image description here



The first slide slider is as far away as it will be. It does not go further to the left, outside the track; it stops when its edge is in the frame of the track. This is zero.

If you don't like it when the thumb image is drawn relative to the common track, then you need to subclass UISlider and implement thumbRectForBounds:trackRect:value:

.

+5


source


I created a similar slider by subclassing UISlider.



//Get thumb rect for larger track rect than actual to move slider to edges
-(CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value {
    UIImage *image = self.currentThumbImage;

    rect.origin.x -= SLIDER_OFFSET;
    rect.size.width += (SLIDER_OFFSET*2);
    CGRect thumbRect = [super thumbRectForBounds:bounds trackRect:rect value:value];
    return CGRectMake(thumbRect.origin.x, rect.origin.y+2, image.size.width, image.size.height);
}

//Make track rect smaller than bounds
-(CGRect)trackRectForBounds:(CGRect)bounds  {
    bounds.origin.x += SLIDER_OFFSET;
    bounds.size.width -= (SLIDER_OFFSET*2);
    CGRect trackRect = [super trackRectForBounds:bounds];

    return CGRectMake(trackRect.origin.x, trackRect.origin.y, trackRect.size.width, trackRect.size.height);
}

      

-2


source







All Articles