Swift - 即使可见,也不显示/更新控件

时间:2015-07-10 05:03:31

标签: ios swift uibutton uiactivityindicatorview

为可怕的头衔道歉,但我真的无法想出任何更好的方式。我的代码如下:

@IBAction func btnSaveTapped(sender: UIButton) {
    println("Save Button Pressed")
    self.StartAI()
    self.btnSave.enabled = false
    self.AnimateButton(sender)

    var booContinueWithSend:Bool = true
    (Continues)

当我运行上面的代码时,屏幕上的控件没有变化。我已经逐步完成了代码和每一行,没有任何变化。即使是btnSave.enabled,也没有变化。我已通过po控制台调试器确认该按钮未启用,但它仍然显示为好像。是的,我已将其设置为根据状态(下面的代码)明显改变:

self.btnSave.setTitleColor(conButtonTextColour, forState: UIControlState.Normal)
self.btnSave.setTitleColor(conButtonTextColourDisabled, forState: UIControlState.Disabled)

其中conButtonTextColourUIColor.blackColorconButtonTextColourDisabled为灰色。

这也适用于我的活动指示器,我已成功手动测试。当我运行上面的代码(btnSaveTapped)时,没有任何反应(它不会像它应该那样显示)。我在这里以编程方式创建AI:

func CreateAI() {
    let conAIViewHeightWidth:CGFloat = 100
    let conHeightWidth:CGFloat = 50

    self.aiView.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(self.aiView)
    self.aiView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.3)
    self.aiView.layer.cornerRadius = 10

    var constraintViewHeight:NSLayoutConstraint = NSLayoutConstraint(item: self.aiView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: conAIViewHeightWidth)
    var constraintViewWidth:NSLayoutConstraint = NSLayoutConstraint(item: self.aiView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: conAIViewHeightWidth)
    self.aiView.addConstraints([constraintViewHeight, constraintViewWidth])

    var constraintViewX:NSLayoutConstraint = NSLayoutConstraint(item: self.aiView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
    var constraintViewY:NSLayoutConstraint = NSLayoutConstraint(item: self.aiView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
    self.view.addConstraints([constraintViewX, constraintViewY])


    self.aiActivityIndicator.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.aiView.addSubview(self.aiActivityIndicator)
    self.aiActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge


    var constraintX:NSLayoutConstraint = NSLayoutConstraint(item: self.aiActivityIndicator, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.aiView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
    var constraintY:NSLayoutConstraint = NSLayoutConstraint(item: self.aiActivityIndicator, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.aiView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
    self.view.addConstraints([constraintX, constraintY])

    self.aiActivityIndicator.hidesWhenStopped = true
    self.aiActivityIndicator.startAnimating()

    StopAI()
}

这是我分别启动和停止AI的两个例程。因为AI是aiView的子视图,所以我只是隐藏或取消隐藏。然而,有了这些问题,我决定变得超级安全并确保我取消隐藏并启用AI。

func StartAI() {
    self.aiView.hidden = false
    self.aiActivityIndicator.hidden = false
    self.aiActivityIndicator.startAnimating()
    println("Started AI")
}

func StopAI() {
    self.aiView.hidden = true
    println("Stopped AI")
}

我做错了什么? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

试试这段代码:

view.layoutIfNeeded()

在进行所有更改后实施此操作