动态更改 UITextField 宽度取决于内容 [英] Change UITextField width dynamically depends of content

查看:29
本文介绍了动态更改 UITextField 宽度取决于内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和这里有同样的问题:

和约束:

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

解决方案

要使用 Autolayout 做到这一点,您需要对文本字段的宽度进行约束.然后,当文本更改时,计算所需的宽度并将约束上的 constant 属性设置为该值.最后,您需要在父视图上调用 layoutIfNeeded().

I have the same issue as here: Resize a UITextField while typing (by using Autolayout)

But It was resolved with UITextView and not with UITextField.

This code doesn't help:

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

I have UITextField(red background) and use AutoLayout with StackView:

and constraints:

but UITextField change it width only when it is resignFirstResponder. Could you give me please any advice about the issue? I'm new in Swift and iOS Development.

解决方案

To do this using Autolayout, you will need a constraint for the width of the text field. Then, when the text changes, calculate the desired width and set the constant property on the constraint to that value. Lastly, you will need to call layoutIfNeeded() on the parent view.

这篇关于动态更改 UITextField 宽度取决于内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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