iOS13之后的分段控制器背景为灰色 [英] Segmented Controller background grey after iOS13
问题描述
我目前在使用iOS 13分段控制器时遇到问题.我有这种方法可以更改分段控制器的外观,在iOS 13退出之前,它一直很好用.现在,即使我将背景色设置为白色,黑色或其他任何颜色,segmentedController始终具有灰色背景.
I'm currently having problems with an iOS 13 segmented controller. I have this method in which I change the appearance of my segmented controller, it worked great until iOS 13 got out. now I the segmentedController has a grey background always, even if I set the background color to white, or black or whatever.
我该怎么办?
- (void)modifySegmentedControl{
if (@available(iOS 13.0, *)) {
[_segmentedControl setBackgroundColor:UIColor.clearColor];
[_segmentedControl setSelectedSegmentTintColor:UIColor.clearColor];
} else {
//I had this for <iOS13, it works great
[_segmentedControl setBackgroundColor:UIColor.clearColor];
[_segmentedControl setTintColor:UIColor.clearColor];
}
[_segmentedControl setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:0.25 green:0.25 blue:0.25 alpha:0.6], NSForegroundColorAttributeName,
[UIFont fontWithName:@"Poppins-Medium" size:15.0], NSFontAttributeName,
nil]
forState: UIControlStateNormal];
[_segmentedControl setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:0.25 green:0.25 blue:0.25 alpha:1.0], NSForegroundColorAttributeName,
[UIFont fontWithName:@"Poppins-Medium" size:15.0], NSFontAttributeName,
nil]
forState: UIControlStateSelected];
self->greenBar = [[UIView alloc] init];
//This needs to be false since we are using auto layout constraints
[self->greenBar setTranslatesAutoresizingMaskIntoConstraints:NO];
[self->greenBar setBackgroundColor:[UIColor colorWithRed:0.00 green:0.58 blue:0.27 alpha:1.0]]; //Kelley green
//
[_vistaTable addSubview:self->greenBar];
//
[self->greenBar.topAnchor constraintEqualToAnchor:_segmentedControl.bottomAnchor].active = YES;
[self->greenBar.heightAnchor constraintEqualToConstant:3].active = YES;
[self->greenBar.leftAnchor constraintEqualToAnchor:_segmentedControl.leftAnchor].active = YES;
[self->greenBar.widthAnchor constraintEqualToAnchor:_segmentedControl.widthAnchor multiplier:0.5].active = YES;
}
推荐答案
尝试一下:
if (@available(iOS 13.0, *)) {
self.segmentedControl.selectedSegmentTintColor = UIColor.redColor;
self.segmentedControl.layer.backgroundColor = UIColor.greenColor.CGColor;
}
.selectedSegmentTintColor
定义所选按钮的颜色,而.layer.backgroundColor
定义整个UISegmentedControl背景的颜色.
.selectedSegmentTintColor
defines the selected button color and .layer.backgroundColor
the color for the whole UISegmentedControl background.
这是结果:
编辑
事实证明,这对于背景清晰或白色不起作用,因为在iOS 13上,在分段控件的背景和分隔线上添加了一种背景图像:
It turns out this won't work for background clear or white, since on iOS 13 a sort of background image is added on the background and dividers of the segmented control:
一种工作方式是使用UIGraphicsGetImageFromCurrentImageContext
从颜色创建图像.代码如下:
A workaroud is create an image from color with UIGraphicsGetImageFromCurrentImageContext
. The code looks like this:
- (void)viewDidLoad {
[super viewDidLoad];
if (@available(iOS 13.0, *)) {
self.segmentedControl.selectedSegmentTintColor = UIColor.redColor;
self.segmentedControl.layer.backgroundColor = UIColor.clearColor.CGColor;
[self customizeSegmentedControlWithColor: UIColor.whiteColor];
}
}
- (void)customizeSegmentedControlWithColor:(UIColor *)color {
UIImage *tintColorImage = [self imageWithColor: color];
[self.segmentedControl setBackgroundImage:[self imageWithColor:self.segmentedControl.backgroundColor ? self.segmentedControl.backgroundColor : [UIColor clearColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.segmentedControl setBackgroundImage:tintColorImage forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self.segmentedControl setBackgroundImage:[self imageWithColor:[color colorWithAlphaComponent:0.2]] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[self.segmentedControl setBackgroundImage:tintColorImage forState:UIControlStateSelected|UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self.segmentedControl setDividerImage:tintColorImage forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
self.segmentedControl.layer.borderWidth = 1;
self.segmentedControl.layer.borderColor = [color CGColor];
}
- (UIImage *)imageWithColor: (UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
在此处中查看有关此内容的更多信息.
Check more about it here.
这是带有颜色和分段控件白色的背景视图的结果:
This is the result for background view with color and segmented control white:
是白色背景视图和带有颜色的分段控件的结果:
And is the result for background view white and segmented control with color:
这篇关于iOS13之后的分段控制器背景为灰色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!