0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

UISwitch

Posted at

UISwitch

C524B41B-A5C8-4434-9020-BAAA04DDE1AB_4_5005_c.jpeg iPhoneの設定でよく見かける、このSwitchのことです.

コードで作成

  • Swich自体は、とても簡単に作ることができます
  • あとは、SwitchがONなのOFFなのかで処理をさせたりします。
Switch
import Foundation
import UIKit

class Switch{
    
    let modeCangeSwitch = UISwitch()
    
    func createSwitch(targetView:UIView){
        
        modeCangeSwitch.center = targetView.center
        modeCangeSwitch.onTintColor = .systemGreen 
        
        targetView.addSubview(modeCangeSwitch)
        
    }  
}

簡単な実例

Switch
        if modeCangeSwitch.isOn == true{
          
            //switchがオンの時の処理など  
            
        }else if modeCangeSwitch.isOn == false{
            
            //switchがオフの時の処理など  

        }
  • .isOnは、Switchの状況をBool型で判定をしてくれます。trueならオン、falseならオフとなります。

終わり

普段UISwitchを使ってないので、休憩の時に少し使ってみました。
簡単に実装できて、便利そうですね。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?