简体   繁体   中英

Setting UITextField heightAnchor to 0 does not work on iOS 15

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

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM