iOS 13 introduced some changes to the UISegmentedControl including a really nice animation when switching the selected segment. However I\'m noticing that it\'s not displaying t
In Xamarin.iOS this worked for me:
class MySegmentedControl : UISegmentedControl
{
int insertedIndex = 0;
public override void InsertSubview(UIView view, nint atIndex)
{
base.InsertSubview(view, atIndex);
if (insertedIndex == 2 || insertedIndex == 3)
view.Hidden = true;
insertedIndex++;
}
}
Works for me (Swift 5).
let background = myColors.background
let selectedColor = myColors.foreground
if #available(iOS 13.0, *)
{
segmentedControl.tintColor = background
segmentedControl.backgroundColor = background
segmentedControl.selectedSegmentTintColor = selectedColor
segmentedControl.setTitleTextAttributes([.foregroundColor: selectedColor as Any], for: .normal)
segmentedControl.setTitleTextAttributes([.foregroundColor: background as Any], for: .selected)
}
else
{
segmentedControl.tintColor = background
segmentedControl.backgroundColor = selectedColor
segmentedControl.layer.cornerRadius = 4
}
SWIFT 3 & 4+
From this answer https://stackoverflow.com/a/31652184/3249196 , if you want an all white background without the grey overlay justr replace tintColor
and backgroundColor
with UIColor.white
extension UISegmentedControl {
func removeBorders() {
setBackgroundImage(imageWithColor(color: backgroundColor!), for: .normal, barMetrics: .default)
setBackgroundImage(imageWithColor(color: tintColor!), for: .selected, barMetrics: .default)
setDividerImage(imageWithColor(color: UIColor.clear), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
}
// create a 1x1 image with this color
private func imageWithColor(color: UIColor) -> UIImage {
let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context!.setFillColor(color.cgColor);
context!.fill(rect);
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image!
}
}
I have the same issue and there is no cool way to resolve it. So I did this small workaround. I dont like it and I am not proud of it, but it works.
func fixBackgroundSegmentControl( _ segmentControl: UISegmentedControl){
if #available(iOS 13.0, *) {
//just to be sure it is full loaded
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
for i in 0...(segmentControl.numberOfSegments-1) {
let backgroundSegmentView = segmentControl.subviews[i]
//it is not enogh changing the background color. It has some kind of shadow layer
backgroundSegmentView.isHidden = true
}
}
}
}