【Swift】UISwitchの色を変更する


どういうことか

コレ

――の、 ◯ の部分とかの色を変えたい。

やる

@IBOutlet weak var toggleSwitch: UISwitch!

// 中略

// ◯の部分
toggleSwitch.thumbTintColor = UIColor.black
// ◯じゃない部分
toggleSwitch.onTintColor = UIColor.orange

たとえばスイッチオンのときだけ色を変えたい場合はif文を使えばよい。

// メンバ変数
var toggleSwitchCheck = false

// 中略

if toggleSwitchCheck {
    toggleSwitch.setOn(false, animated:true)
    toggleSwitchCheck = false
    toggleSwitch.thumbTintColor = UIColor.white
} else {
    toggleSwitch.setOn(true, animated:true)
    toggleSwitchCheck = true
    toggleSwitch.thumbTintColor = UIColor.orange
}

完(´・ω・`)