StackView isHidden 属性未按预期更新 [英] StackView isHidden attribute not updating as expected

查看:23
本文介绍了StackView isHidden 属性未按预期更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新 UIStackView 以便显示一个字段,如果 UITextField 的值等于 "Other".这是我的代码:

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

@IBOutlet var stackView: UIStackView!
func updateView() {
    print("UPDATING")
    UIView.animate(withDuration: 0.25, animations: { () -> Void in
         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 )")
    })

示例输出如下所示:

> UPDATING
> HIDING
> Is hidden: true
> UPDATING
> SHOWING
> Is hidden: true

如您所见,isHidden 属性报告为 true,无论上面的代码将其设置为什么.我真的无法弄清楚为什么会这样,但也许这里的某个人可以?有什么明显的要检查吗?isHidden 无法更新有什么原因吗?(注意输出中没有出现错误).

As you can see, the isHidden attribute is reported as true, regardless of what the code above has set it to. I can't really figure out why that might be, but perhaps someone on here can? Is there something obvious to check? Is there any reason why isHidden can't be updated? (note there are no errors appearing in the output).

推荐答案

用户界面的更新总是必须在主线程上完成(THE LAW).

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

因此将 UI 更新包装在主标题上:

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

这篇关于StackView isHidden 属性未按预期更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆