我知道这是5年前,但它可能会帮助别人,谁喜欢我偶然发现了你的问题。
我用一个UIToolbar作为我inputAccessoryView(在我的情况下,它有一个标签,一个灵活的分离器和按钮)。 在textFieldEditingChanged事件我重建工具栏的一部分,这样
@IBAction func textFieldEditingChanged(_ sender: UITextField) {
//get a reference to the toolbar
let toolBar = sender.inputAccessoryView as! UIToolbar
//create a new label and set size + other properties
toolbarLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 40, height: 22))
toolbarLabel.text = mySpecificCalculatedString(sender.text!)
toolbarLabel.font = defaultFont(17)
toolbarLabel.adjustsFontSizeToFitWidth = true
let width = toolbarLabel.textRect(forBounds: CGRect(x: 0, y: 0, width: maxWidthForLabel, height: 0), limitedToNumberOfLines: 1).size.width
toolbarLabel.frame = CGRect(x: 0, y: 0, width: width, height: 22)
toolbarLabel.textColor = .black
toolbarLabel.tag = 666
//rebuild the specific tolbar item
let customView = UIBarButtonItem(customView: toolbarLabel)
toolBar.items![0] = customView
}
注:简单地改变标签的文本对我也没有工作,我不得不重新初始化。
希望它有帮助。
你是如何试图更新标签,以及如何你肯定的是,即使用户界面不显示它的价值被更新? – 2011-06-04 16:03:23
请显示更多代码。 – jaminguy 2011-06-04 16:45:25