UISegmented控件的最后一段颜色中断

时间:2018-07-19 18:55:19

标签: ios objective-c uisegmentedcontrol

我似乎对UISegmented控件在最后一个片段上破坏其颜色有问题。这是一个例子

enter image description here

我使用UIStoryboard来设置我的分段控件,但是对于颜色更改和边框半径,这就是我正在使用的

_segmentedControl.layer.masksToBounds = YES;
_segmentedControl.layer.cornerRadius = 20;
_segmentedControl.layer.borderColor = [UIColor whiteColor].CGColor;
[_segmentedControl setSelectedSegmentIndex:UISegmentedControlNoSegment];
[_segmentedControl setBackgroundImage:[Painter imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[_segmentedControl setBackgroundImage:[Painter imageWithColor:_segmentedControl.tintColor] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

有关此imageWithColor在做什么的其他信息,请参见

+(UIImage*)imageWithColor:(UIColor*)color{
   CGRect rect = CGRectMake(0, 0, 2.0, 2.0);
   UIGraphicsBeginImageContext(rect.size);
   CGContextRef context = UIGraphicsGetCurrentContext();
   CGContextSetFillColorWithColor(context, color.CGColor);
   CGContextFillRect(context, rect);
   UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   return image;
}

1 个答案:

答案 0 :(得分:0)

我使用了相同的代码,结果如下:

enter image description here

没有白色边框。

我正在将XCode 9.2与iOS11 iPhoneX模拟器一起使用。

您可以尝试更改代码

_segmentedControl.layer.borderColor = [UIColor whiteColor].CGColor;

收件人:

_segmentedControl.layer.borderColor = [UIColor clearColor].CGColor;

如果问题仍然存在,您可以尝试使用相同的地雷配置,如果问题仍然存在,我们可以尝试找到解决方案。

编辑

好的,我尝试过使用您的宽度,我可以重现该问题,但只有在设置了比例宽度后才能重现该问题。

这是因为比例宽度可以用十进制数字设置宽度大小,所以我们看到的是大小的小数部分。

好消息是,这似乎只是模拟器问题,我已经将该项目部署在iPhone中,并且无法复制。

您可以在IB的大小检查器中尝试更改比例宽度,直到在“视图宽度”中看到不带小数的整数。

相关问题