自定义后UISegmentedControl搞砸了

时间:2013-08-26 15:22:55

标签: ios uisegmentedcontrol

所以在我自定义了UISegmentedControl之后,一切都搞砸了,标签被移动了,还有一条白色的垂直线,我不知道它来自哪里。分频器也偏离中心,我检查过黑线是80px图像的40-41px,因此它必须居中。我想做错了还是有人经历过这个?

自定义之前: enter image description here

自定义后:

注意:分频器图像是彼此水平翻转。

enter image description here

enter image description here

代码:

UIImage *un_sel = [UIImage imageNamed:@"divider-un-sel"];
UIImage *sel_un = [UIImage imageNamed:@"divider-sel-un"];

[_segmentControl setDividerImage:un_sel forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

[_segmentControl setDividerImage:sel_un forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

UIImage *normal = [[UIImage imageNamed:@"segment-normal"] stretchableImageWithLeftCapWidth:20 topCapHeight:0];
UIImage *selected = [[UIImage imageNamed:@"segment-selected"] stretchableImageWithLeftCapWidth:20 topCapHeight:0];

[_segmentControl setBackgroundImage:normal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[_segmentControl setBackgroundImage:selected forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

1 个答案:

答案 0 :(得分:0)

使用

解决了问题
[[UIImage imageNamed:@"segment-normal"]  resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 15)];

而不是stretchableImageWithLeftCapWith:topCapHeight