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.
What Can I do?
- (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;
}
A UISegmentedControl has a new appearance in iOS 13 and existing code to alter the colors of the segmented control no longer work as they did. Prior to iOS 13 you could set the tintColor and that ...
There is no border color in iOS 13 and in iOS 12 it's set with tintColorwhich is already covered in the answer. – rmaddy Sep 18 '19 at 6:01
In iOS 13 and above, when I accessed the segmented control's subviewsin viewDidLoad, it has 1 UIImageView. Once I inserted more segments, it increases respectively, so 3 segments means 3 UIImageViewas the segmented control's subviews.
For people trying to customize a Picker with SegmentedPickerStyle() in SwiftUI in iOS 13 or 14, the simplest option is to use UISegmentedControl.appearance()to set the appearance globally. Here is an example function that could be called to set the appearance.
Try this:
if (@available(iOS 13.0, *)) {
self.segmentedControl.selectedSegmentTintColor = UIColor.redColor;
self.segmentedControl.layer.backgroundColor = UIColor.greenColor.CGColor;
}
.selectedSegmentTintColor
defines the selected button color and .layer.backgroundColor
the color for the whole UISegmentedControl background.
This is the result:
EDIT
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:
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With