ios - iOS13后Segmented Controller背景灰
问题描述
我目前在使用 iOS 13 分段控制器时遇到问题。我有这种方法可以更改分段控制器的外观,在 iOS 13 发布之前效果很好。现在我的 segmentedController 总是有灰色背景,即使我将背景颜色设置为白色、黑色或其他。
我能做些什么?
- (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 背景的颜色。
这是结果:
编辑
事实证明,这不适用于背景清晰或白色,因为在 iOS 13 上,在分段控件的背景和分隔符上添加了一种背景图像:
一种解决方法是使用UIGraphicsGetImageFromCurrentImageContext
. 代码如下所示:
- (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;
}
在这里查看更多信息。
这是具有颜色和分段控件白色的背景视图的结果:
并且是背景视图白色和带颜色的分段控件的结果:
推荐阅读
- mysql - 多个插入查询不适用于带有 MySQL 的 TSQLQuery
- c# - C# 从文本框中获取变量并在 ExecuteNonQuery 中用作变量
- c# - 即使作为参数传递,图像也不会出现在 rdlc 报告中
- javascript - Javascript for循环没有在另一个for循环中获得长度
- javascript - 遍历 Html 中的 id
- javascript - Angular 在 component.ts 中存储嵌套数组
- c - 指向指针分段错误的指针
- angularjs - 角度,选择动态禁用
- c# - c# - 使用索引数组反序列化 JSON
- sql-server - 使用从原始表中移动分区数据创建分区