简体   繁体   English

对齐UISlider拇指图像 - objective-c

[英]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: 我已经定制了我的UISlider但我的拇指图像看起来很奇怪,我的意思是它的位置没有按中心对齐:

在此输入图像描述

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 UISlider方法,当子类化以你想要的方式布局拇指

Second way is to adjust your artwork: 第二种方法是调整你的艺术作品:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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