2017-05-21 1 views
0

This is the image of my current segmented control, the 2nd section is currently selected.UISegmentedControl国境/ハイライトされた状態

私は現在、私のVCでuisegmentedcontrol要素を持っており、以下のスレッドに従うことによって国境を削除するために管理しています。それがうまくいく間、一度選択すると、アイコンを含むuisegmentedcontrolセクションは完全に白くなりますが、それを変更する方法はありますか?したがって、選択したセクションの背景は、指定した色になります。アイコンは白になります。助けてくれる人には事前に感謝します。

言及スレッド: Swift: How to remove border from segmented control

+0

SegmentedControlのコードとスクリーンショットを共有できますか? –

+0

2番目のセクションが選択されたスクリーンショットを追加しました。コードは私が投稿したリンクにアクセスしてください。これは正確なコードです。 – Blackroche

+0

選択した状態と選択していない状態を使用して別の色を使うことができます。 –

答えて

2

これはあなたが色に基づいて背景画像を設定している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の状態。だからここで好きな色を使うことができます。

+0

ありがとうございます!私は私のPCに戻ってすぐに私はそれをショットを与えるよ、もう一度おかげで! – Blackroche

+0

それは働いている!ありがとうございました! :) – Blackroche

+0

ありがとう私の喜び:) –

関連する問題