On iOS 12, to get a UISegmentedControl with clear border, clear divider line, everything clear was easy. All I did was this:
settingControl.tintColor = .clear
let font = myFont
let boldfont = myBoldFont
settingControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white, NSAttributedString.Key.font:font], for: .normal)
settingControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.red, NSAttributedString.Key.font:boldfont], for: .selected)
And then UISegmentedControl was full clear color (divider line, background, border)
But in iOS 13, I can not get it fully clear. I can set
settingControl.selectedSegmentTintColor = UIColor.clear
But it still does not clear the backgroundColor and divider line.
I tried setting backgroundColor to clear, but no effect.
settingControl.backgroundColor = UIColor.clear
I also tried setting a clear image but still nothing:
public extension UIImage {
/**
Returns image with size 1x1px of certain color.
*/
class func imageWithColor(color : UIColor) -> UIImage? {
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context?.setFillColor(color.cgColor)
context?.fill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
And then this:
let clearImage = UIImage.imageWithColor(color: UIColor.clear)
settingControl.setDividerImage(clearImage, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
settingControl.setBackgroundImage(clearImage, for: .normal, barMetrics: .default)
settingControl.setBackgroundImage(clearImage, for: .selected, barMetrics: .default)
This is how it looks like on iOS 12. On iOS 13, this seems impossible.
See Question&Answers more detail:
os