通常Segmented Controlには赤枠で囲んだ仕切り線が付いているので、これを消したい。
だが、Segmented Controlにはそれを隠すようなプロパティはないようだった。
ただ調べるとsetDividerImage(_:forLeftSegmentState:rightSegmentState:barMetrics:)というメソッドがあり、これを使ってセパレーター画像を設定できるらしい。
なので下記のコードでSegmentd Controlの背景と同じ色のセパレーター画像を生成し設定することでを見えないように(背景色と同化)することにした。
.swift
//色はデフォルトのセパレーターと同じ設定
//widthを1にしheightをSegmented Controllerと同じ高さに設定
func removeSegmentSeprator(height: CGFloat) -> UIImage {
UIGraphicsBeginImageContext(CGSize(width: 1, height: height))
let context: CGContext = UIGraphicsGetCurrentContext()!
let backGroundColor = UIColor(red: 238/255, green: 238/255, blue: 239/255, alpha: 1)
context.setFillColor(backGroundColor.cgColor)
context.fill(CGRect(x: 0, y: 0, width: 1, height: height))
guard let image = UIGraphicsGetImageFromCurrentImageContext() else {return UIImage()}
UIGraphicsEndImageContext()
return image
}
let image = removeSegmentSeprator(height: segmentControl.bounds.height)
segmentControl.setDividerImage(image, forLeftSegmentState: .normal,rightSegmentState: .normal, barMetrics: .default)