2017-02-23 7 views
11

これは私のUISegmentedControlは、スウィフトに書かれている:SwiftでユーザーがUISegmentedControlを選択したときに、どのようにスタイルを変更できますか?

enter image description here

私は次のコードでそれを作成した:

let selectedAttributes: NSDictionary = [ 
     NSForegroundColorAttributeName: UIColor.black, 
     NSFontAttributeName: fontForSegmentedControl! 
    ] 

    let normalAttributes: NSDictionary = [ 
     NSForegroundColorAttributeName: UIColor.gray, 
     NSFontAttributeName: fontForSegmentedControl! 
    ] 

    mySegmentedControl.setTitleTextAttributes(selectedAttributes as [NSObject : AnyObject], for: UIControlState.selected) 

    mySegmentedControl.setTitleTextAttributes(normalAttributes as [NSObject : AnyObject], for: UIControlState.normal) 

と国境を除去するための拡張はここにある:

extension UISegmentedControl { 
    func removeBorders() { 
     setBackgroundImage(imageWithColor(color: UIColor.white/*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! 
    } 
} 

しかし、それに間違いが1つあります。

私はタップ(ホールド)ONEまたはTWOそれがこの(時間のためにそれがユーザの指で触れています)に背景色を変更する:私はのためにいくつかのコードを欠けている

enter image description here

UISegmentedControlの選択された(一時的に押された)オプションのスタイルを変更する。

ダークグレーの選択を削除して透明な色のままにするにはどうすればよいですか?すでにUIControlState.normalの背景画像を設定している方法と同様に

答えて

9

は、あなたがUIControlState.highlightedと+ UIControlState.highlightedUIControlState.selectedのための適切な画像を設定する必要があります。

(あなたが押され選択されていないセグメントの明確なバックグラウンドを持ちたいと押すための色合い色の背景には1を選択したと仮定して)あなたの拡張機能removeBorders()に次のコードを追加します

setBackgroundImage(imageWithColor(color: .clear), for: .highlighted, barMetrics: .default) 
setBackgroundImage(imageWithColor(color: tintColor!), for: [.selected, .highlighted], barMetrics: .default) 
+0

私はそれを試してみるとほとんど動作します:)現在、ユーザーが現在選択されていないオプションを押すと、グレーで強調表示されませんが、現在選択されているオプションを押して背景を灰色に変える可能性があります。 ? – user3766930

+1

@ user3766930確かに、あなたはそれを行うことができます、私は答えを更新しました。 –

0
let normalAttributes: NSDictionary = [ 
    NSForegroundColorAttributeName: UIColor.gray, 
    NSFontAttributeName: fontForSegmentedControl! 
] 

mySegmentedControl.setTitleTextAttributes(selectedAttributes as [NSObject : AnyObject], for: UIControlState.selected) 

上記のコードは選択のためのグレーだけを追加するので、以下のように変更してください

let normalAttributes: NSDictionary = [ 
    NSForegroundColorAttributeName: UIColor.white, 
    NSFontAttributeName: fontForSegmentedControl! 
] 

mySegmentedControl.setTitleTextAttributes(selectedAttributes as [NSObject : AnyObject], for: UIControlState.selected) 
関連する問題