-1

I have an iOS application where i have a textfield and a button and on tap of button i have to hide the textfield.

I am setting heightAnchor to 0 on tap of button. Everything is working fine on iOS 14(14.5) but does not work(does not hide the text field) on iOS 15. Also, I have tried setting up the isHidden property on UITextField but it does not work.

Can you please help tell if something changed or i am doing something wrong. Thank you.

Code reference:

import UIKit

class ViewController: UIViewController {

  private lazy var mytextFeild: UITextField = {
    let textField = UITextField()
    textField.translatesAutoresizingMaskIntoConstraints = false
    textField.text = "Hello world"
    textField.backgroundColor = .green
    return textField
  }()
   
  private lazy var testView: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = .systemPink
    return view
  }()
   
  private lazy var button: UIButton = {
    let view = UIButton()
    view.backgroundColor = .blue
    view.setTitle("hide it", for: .normal)
    view.translatesAutoresizingMaskIntoConstraints = false
     
    view.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
    return view
  }()
   
  var heightConstraint: NSLayoutConstraint?
   
  @objc func buttonTapped() {
    heightConstraint?.isActive = false
    heightConstraint = mytextFeild.heightAnchor.constraint(equalToConstant: 0)
    heightConstraint?.isActive = true
  }

  override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(mytextFeild)
    view.addSubview(testView)
    view.addSubview(button)
     
    mytextFeild.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 32).isActive = true
    mytextFeild.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -32.0).isActive = true
    mytextFeild.topAnchor.constraint(equalTo: view.topAnchor, constant: 64).isActive = true
    heightConstraint = mytextFeild.heightAnchor.constraint(equalToConstant: 32.0)
    heightConstraint?.isActive = true
    button.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 32.0).isActive = true
    button.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -32.0).isActive = true
    button.heightAnchor.constraint(equalToConstant: 32.0).isActive = true
    button.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -64.0).isActive = true
     
    testView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    testView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    testView.topAnchor.constraint(equalTo: mytextFeild.bottomAnchor).isActive = true
    testView.bottomAnchor.constraint(equalTo: button.topAnchor).isActive = true
  }
}

enter image description here

1 Answers1

-1

Add them to a stack then add stack to the viewController. at the end try to hide it easily without changing the height.

class ViewController: UIViewController {

    private lazy var stackView: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.distribution = .fill
        stackView.alignment = .center
        stackView.translatesAutoresizingMaskIntoConstraints = false
        return stackView
    }()
    
    private lazy var myTextField: UITextField = {
        let textField = UITextField()
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.text = "Hello world"
        textField.backgroundColor = .green
        return textField
    }()
    
    private lazy var testView: UIView = {
        let view = UIView(frame: .zero)
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .systemPink
        return view
    }()
    
    private lazy var button: UIButton = {
        let view = UIButton()
        view.backgroundColor = .blue
        view.setTitle("hide it", for: .normal)
        view.translatesAutoresizingMaskIntoConstraints = false
        
        view.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        return view
    }()

    @objc func buttonTapped() {
        myTextField.isHidden = !myTextField.isHidden
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        
        stackView.addArrangedSubview(myTextField)
        stackView.addArrangedSubview(testView)
        stackView.addArrangedSubview(button)
        view.addSubview(stackView)
        
        stackView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        stackView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -64.0).isActive = true
        stackView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
        stackView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true

        myTextField.heightAnchor.constraint(equalToConstant: 32.0).isActive = true
        myTextField.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -64.0).isActive = true

        testView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
        
        button.heightAnchor.constraint(equalToConstant: 32.0).isActive = true
        button.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -64.0).isActive = true
    }
}