【发布时间】:2016-06-03 04:53:27
【问题描述】:
我有一个标签,每次用户单击按钮时,它的文本都会发生变化。但是,即使标签内容发生了变化,Label.frame.height 值也不会立即更新。更改标签的函数在多个地方调用,但高度仅在 @IBAction 块内更新,其值滞后于单击周期。我的代码如下:
func changeLabelText() {
//Here I have an algorithm (not shown) that generates myMutableString
Label.attributedText = myMutableString //Label is updated.
}
@IBAction func changeLabelButton(sender: UIButton) {
print("1. Height = ", Label.frame.height) //Height updates here, but it's the old value.
changeLabelText() //Label is updated.
print("2. Height = ", Label.frame.height) //Returns same height as Line 1!!
}
override func viewDidLoad() {
super.viewDidLoad()
print("3. Height = ", Label.frame.height) //Initially, height is 21.0 when ViewController first loads.
changeLabelText() //Label is updated.
print("4. Height = ", Label.frame.height) //Returns height = 21.0, even though simulator shows Label updated!!
}
总而言之,这就是正在发生的事情:
用户点击Button,Label显示新文本,但frame.height不变。
用户再次单击 Button,Label 文本再次更改,frame.height 这次更新,但更新为在步骤 1 中应该具有的旧值。
我对 Swift 比较陌生,如果能提供任何帮助,我们将不胜感激。
【问题讨论】:
标签: ios swift label height frame