简体   繁体   中英

Align UISlider thumb image - objective-c

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

在此输入图像描述

But should be like this:

在此输入图像描述

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];

Where could be the problem?

EDIT: I found solution of this problem. Thumb image background was transparent, and I've not seen that thumb image was not aligned by center:

在此输入图像描述

There are two ways:

Firs one is to use

- (CGRect)trackRectForBounds:(CGRect)bounds;

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:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM