LoginSignup
6
1

More than 5 years have passed since last update.

swift4 UISwitch OFFの時の色変更

Posted at

実行環境:Xcode10.1
使用言語:Swift4.2

sumple.swift
        /// 色をカスタマイズ1
        switch1.onTintColor = UIColor.blue    /// ONの場合の背景色
        switch1.tintColor = UIColor.darkGray    /// OFFの場合の縁取りの色
        switch1.backgroundColor = UIColor.lightGray    /// OFFの場合の背景色
        /// 背景色はview全体にかかるので角丸にして同じようにする
        switch1.layer.cornerRadius =  switch1.frame.size.height / 2

        /// 色をカスタマイズ2
        swich2.onTintColor = UIColor.white
        swich2.tintColor = UIColor.darkGray
        swich2.backgroundColor = UIColor.red
        swich2.layer.cornerRadius =  swich2.frame.size.height / 2

上の値を設定して実行するとこんな感じ

Simulator Screen Shot - iPhone XS Max - 2018-12-27 at 12.09.35.png

色をカスタマイズ2の方は微妙に背景色がはみ出ているので、使用する色によっては使えない...
ON時が濃い色、OFF時が薄い色ならこれでいいかも
どうしても気になる人はいい感じにマスクするなりしてあげてください。
簡単にもっと綺麗にできないものか...

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1