简体   繁体   English

自定义UISegmentedControl问题(以编程方式和界面生成器)

[英]Custom UISegmentedControl Issue (Programmatically & Interface Builder)

I am creating a UISegmentedControl with custom images for each segment. 我用每个段的自定义图像创建一个UISegmentedControl。 Programmatically, this is working with me: 以编程方式,这正在与我合作:

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;

However, the segment's custom image does not fill the segment entirely and the background shows a little. 但是,该段的自定义图像不能完全填充该段,并且背景显示得有些许。 The problem is that the background image does not have the same colour so it looks weird as shown below. 问题在于背景图像的颜色不同,因此看起来很奇怪,如下所示。 You can see a little black around each segment. 您可以在每个分段周围看到一些黑色。

这个

I tried using UIViewContentModeScaleToFill but that does not work either. 我尝试使用UIViewContentModeScaleToFill但这也不起作用。 Is there a way to set a segment's image size? 有没有办法设置细分的图片尺寸? If so, how? 如果是这样,怎么办? And to what size does it have to be? 大小必须是多少?

Try to use the - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets method. 尝试使用- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets方法。 In your case that could be: 在您的情况下,可能是:

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

Please note that you need to determine the correct values for the UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right); 请注意,您需要确定UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);的正确值UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right); function 功能

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

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