Home > Software design >  How to hide/show text field in swift using a switch [duplicate]
How to hide/show text field in swift using a switch [duplicate]

Time:10-02

How can I use a switch to hide/show a text field in Swift? and how can I make the text field area be hidden and shown dynamically? btw: I'm working on Swift storyboard.

CodePudding user response:

First take Outlet of textfield. Then show and hide textfield on switch's ON & OFF State.

CodePudding user response:

@IBOutlet weak var stateSwitch:  UISwitch!
@IBOutlet var textField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()     
stateSwitch.addTarget(self, action: #selector(stateChanged), for: .valueChanged)
}

@objc func stateChanged(switchState: UISwitch) {
if switchState.isOn {
   textField.isHidden = false
} else {
    textField.isHidden = true
}
}
  • Related