これはあなたが色に基づいて背景画像を設定しているremoveBorders
機能で見ることができるようにあなたがUISegmentControl
extension UISegmentedControl{
func removeBorders() {
setBackgroundImage(imageWithColor(color: .clear), for: .normal, barMetrics: .default)
setBackgroundImage(imageWithColor(color: .gray), 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!
}
}
で選択した指標の背景色を変更する方法ですnormal
およびselected
の状態。だからここで好きな色を使うことができます。
SegmentedControlのコードとスクリーンショットを共有できますか? –
2番目のセクションが選択されたスクリーンショットを追加しました。コードは私が投稿したリンクにアクセスしてください。これは正確なコードです。 – Blackroche
選択した状態と選択していない状態を使用して別の色を使うことができます。 –