Timur Mustafaev
Timur Mustafaev

Reputation: 4929

Align UISlider thumb image

I've customized my UISlider but my thumb image looks strange, I mean it's position not aligned by center:

enter image description here

But should be like this:

enter image description here

And here is code:

UIImage *leftTrack = [[UIImage imageNamed:@"blueTrack.png"] stretchableImageWithLeftCapWidth:3 topCapHeight:0];
UIImage *rightTrack = [[UIImage imageNamed:@"whiteTrack.png"] stretchableImageWithLeftCapWidth:3 topCapHeight:0];
[slider setThumbImage:[UIImage imageNamed:@"thumbButton.png"] forState:UIControlStateNormal];
[slider setMinimumTrackImage:leftTrack forState:UIControlStateNormal];
[slider setMaximumTrackImage:rightTrack forState:UIControlStateNormal];

Upvotes: 10

Views: 4442

Answers (1)

Sash Zats
Sash Zats

Reputation: 5466

There are two ways:

First one is to use

- (CGRect)trackRectForBounds:(CGRect)bounds;

and / or

- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value;

methods of UISlider when subclassing to lay out thumb the way you want


Second way is to adjust your artwork:

enter image description here

Upvotes: 6

Related Questions