问题
I'm trying to hide a username textfield when toggling over to login from register and show it again when toggling back. I'm programming the UI in and I would like to figure out how you can hide a textfield, I'm new to swift and I think there are two places I could insert code to hide the username textfield. If not please tell me, Thanks!
Image of what I am trying to hide.
Here in the UISegmentedControl
lazy var loginRegisterSegmentControl: UISegmentedControl = {
let sc = UISegmentedControl(items: ["Login", "Register"])
sc.translatesAutoresizingMaskIntoConstraints = false
sc.tintColor = UIColor.white
sc.selectedSegmentIndex = 1
sc.addTarget(self, action: #selector(handleLoginRegisterChange), for: .valueChanged)
return sc
}()
Or here in the HeightAnchor Change
nameTextFieldHeightAnchor?.isActive = false
nameTextFieldHeightAnchor = nameTextField.heightAnchor.constraint(equalTo: inputContainerView.heightAnchor, multiplier: loginRegisterSegmentControl.selectedSegmentIndex == 0 ? 0 : 1/3)
nameTextFieldHeightAnchor?.isActive = true
回答1:
Use this to set when you want the UITextField
hidden or visible depending on your application structure
You can do it this way to make the field visible:
myTextField.isHidden = false
and this way to make it hidden:
myTextField.isHidden = true
回答2:
Updated for Swift :
Used below simple code :
// UIButtonOutlet
@IBOutlet var confirmPassTextField: UITextField!
@IBOutlet var confirmPassButton: UIButton!
//IBAction Method
@IBAction func actionOnConfirmButton(_ sender: Any) {
if (confirmPassTextField.isSecureTextEntry == true){
confirmPassTextField.isSecureTextEntry = false
confirmPassButton.setImage(UIImage(named: "show_pass"), for: .normal)
}else{
confirmPassButton.setImage(UIImage(named: "hide_pass"), for: .normal)
confirmPassTextField.isSecureTextEntry = true
}
}
Note: "show_pass" and "hide_pass" - UIImage Name
来源:https://stackoverflow.com/questions/39981933/toggling-uitextfield-to-show-and-hide-in-swift