How to increase width of textfield according to typed text?

前端 未结 8 1904
失恋的感觉
失恋的感觉 2020-12-09 04:22

I need to increase a text field\'s width according to its content. When the user inputs text, then the textfield size should increase automatically. I have one close (X) but

8条回答
  •  独厮守ぢ
    2020-12-09 04:40

    The answers all require a bunch of code, but you can do everything interface builder; no code needed.

    Just embed the textField in a UIStackView and constrain the stackView to be less than or equal to its superview minus some constant (if you want you can also give it a minimum width). The stackView will take care of always making the textField its intrinsic size, or the maxwidth of the stackView (which ever is smaller), so as you type the size changes automatically to fit the content.

提交回复
热议问题