StackView isHidden attribute not updating as expected

前端 未结 3 1410
生来不讨喜
生来不讨喜 2021-02-05 08:37

I\'m trying to update a UIStackView so that a field displays, should the value of a UITextField equal \"Other\". Here is my code:

3条回答
  •  野的像风
    2021-02-05 09:05

    Updates on the user interface always have to be done on the main thread (THE LAW).

    So wrap you UI updates on the main thead:

    @IBOutlet var stackView: UIStackView!
    func updateView() {
        print("UPDATING")
        UIView.animate(withDuration: 0.25, animations: { () -> Void in
            DispatchQueue.main.async {  // UI updates on the main thread
                if(self.myTextField.text! == "Other") {
                    print("SHOWING")
                    self.stackView.arrangedSubviews[3].isHidden = false
                 } else {
                    print("HIDING")
                    self.stackView.arrangedSubviews[3].isHidden = true
                 }
                 print("Is hidden: \(self.stackView.arrangedSubviews[3].isHidden )")
            }
        })
    

提交回复
热议问题