我目前在使用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;
}
2条答案
按热度按时间mzmfm0qo1#
试试这个:
.selectedSegmentTintColor
定义所选按钮的颜色,.layer.backgroundColor
定义整个UISegmentedControl背景的颜色。结果如下:
编辑
事实证明,这对背景清晰或白色不起作用,因为在iOS 13上,在分段控件的背景和分隔符上添加了一种背景图像:
一个变通方法是使用
UIGraphicsGetImageFromCurrentImageContext
从颜色创建图像。代码看起来像这样:查看更多here
这是带有彩色和分段控制白色的背景视图的结果:
并且是背景视图白色和带颜色的分段控制的结果:
sycxhyv72#
您必须隐藏添加该色调的默认
UIImageView
:测试和工作在iOS 13.1 -> 16.5