これは私のUISegmentedControl
は、スウィフトに書かれている:SwiftでユーザーがUISegmentedControlを選択したときに、どのようにスタイルを変更できますか?
私は次のコードでそれを作成した:
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
それがこの(時間のためにそれがユーザの指で触れています)に背景色を変更する:私はのためにいくつかのコードを欠けている
UISegmentedControl
の選択された(一時的に押された)オプションのスタイルを変更する。
ダークグレーの選択を削除して透明な色のままにするにはどうすればよいですか?すでにUIControlState.normal
の背景画像を設定している方法と同様に
私はそれを試してみるとほとんど動作します:)現在、ユーザーが現在選択されていないオプションを押すと、グレーで強調表示されませんが、現在選択されているオプションを押して背景を灰色に変える可能性があります。 ? – user3766930
@ user3766930確かに、あなたはそれを行うことができます、私は答えを更新しました。 –