gpt4 book ai didi

ios - 根据内容动态更改 UITextField 宽度

转载 作者:可可西里 更新时间:2023-11-01 00:36:33 25 4
gpt4 key购买 nike

我和这里有同样的问题:Resize a UITextField while typing (by using Autolayout)

但它是通过 UITextView 而不是通过 UITextField 解决的。

这段代码没有帮助:

func textFieldDidChange(sender: UITextField) {
sender.invalidateIntrinsicContentSize()
}

我有 UITextField(红色背景)并将 AutoLayout 与 StackView 一起使用:

enter image description here

和约束:

enter image description here

但是 UITextField 只有在它是 resignFirstResponder 时才改变它的宽度。你能给我关于这个问题的任何建议吗?我是 Swift 和 iOS 开发的新手。

最佳答案

UITextField 在更改内容时不会调用您的方法 textFieldDidChange(_:)(它不是 UITextFieldDelegate 的一部分)。您应该改用 textField(_:shouldChangeCharactersInRange:replacementString:)

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
textField.invalidateIntrinsicContentSize()
return true
}

关于ios - 根据内容动态更改 UITextField 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37355212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com