【问题标题】:Toggling UITextField to show and hide in Swift切换 UITextField 以在 Swift 中显示和隐藏
【发布时间】:2017-02-20 06:31:06
【问题描述】:

我试图在切换到从注册登录时隐藏用户名文本字段,并在切换回来时再次显示它。我正在对 UI 进行编程,我想弄清楚如何隐藏文本字段,我是 swift 新手,我认为有两个地方可以插入代码来隐藏用户名文本字段。如果没有请告诉我,谢谢!

我试图隐藏的图像。

在 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
    }()

或者在HeightAnchor Change中

nameTextFieldHeightAnchor?.isActive = false
        nameTextFieldHeightAnchor = nameTextField.heightAnchor.constraint(equalTo: inputContainerView.heightAnchor, multiplier: loginRegisterSegmentControl.selectedSegmentIndex == 0 ? 0 : 1/3)
        nameTextFieldHeightAnchor?.isActive = true

【问题讨论】:

    标签: ios swift uitextfield show-hide


    【解决方案1】:

    为 Swift 更新:

    在简单代码下面使用:

    // UIButtonOutlet

    @IBOutlet var confirmPassTextField: UITextField!
    @IBOutlet var confirmPassButton: UIButton!
    

    //IBAction方法

    @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
        }
    }
    

    注意:“show_pass”和“hide_pass” - UIImage 名称

    【讨论】:

      【解决方案2】:

      根据您的应用程序结构,使用它来设置您希望 UITextField 隐藏或可见的时间

      您可以通过这种方式使字段可见

      myTextField.isHidden = false

      并且通过这种方式使其隐藏

      myTextField.isHidden = true

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-08
        • 1970-01-01
        • 2015-11-28
        相关资源
        最近更新 更多