0

我正在为每个段创建一个带有自定义图像的 UISegmentedControl。以编程方式,这对我有用:

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(30, 0, 170, 31)];
segmentedControl.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ebd-toolbar-bg"]];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"eatIcon"] atIndex:0 animated:NO];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"buyIcon"] atIndex:1 animated:NO];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"doIcon"] atIndex:2 animated:NO];
segmentedControl.contentMode = UIViewContentModeScaleToFill;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;

但是,该段的自定义图像并没有完全填满该段,并且背景显示了一点。问题是背景图像没有相同的颜色,所以看起来很奇怪,如下所示。您可以在每个段周围看到一点黑色。

这个

我尝试使用UIViewContentModeScaleToFill,但这也不起作用。有没有办法设置段的图像大小?如果是这样,怎么做?它必须有多大?

4

1 回答 1

0

尝试使用该- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets方法。在您的情况下,可能是:

[segmentedControl insertSegmentWithImage:[[UIImage imageNamed:@"eatIcon"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,10,0,10)] atIndex:0 animated:NO];

请注意,您需要确定UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);函数的正确值

于 2013-01-06T14:47:23.610 回答