4

I have a UITextField positioned in a view next to a button. It has a trailing constraint of 8 to the button (which has a trailing constraint of 8 to the superview) and when I type long text in it, it simply scrolls along, which is I want. However, in order to retain the text typed in the field if the view is switched to another one (it's in a tab controller), I save the text in a holder variable and when it switches back to that view, I set the text in the field to the saved text.

The problem is that this causes the field to expand horizontally if the text is long enough, sometimes pushing the button off-screen, even with the trailing 8 constraint. I have tried to save the original frame of the field in a holder variable, and then after setting the text, set the frame to the saved original frame like so:

fieldFrame = messageField.frame
println(messageField.frame.width)
messageField.text = holderMessage
println(messageField.frame.width)
messageField.frame = fieldFrame

However, the field still expands, and it printed out 502.0 twice. My current thought is that the frame hasn't registered the change in width after the setting of the text in time for the println, but I'm not sure if this is correct.

Also, I've read some similar questions that suggested using a width constraint. If I use a less than or equal to width constraint on the field, will it still expand if on a device that's thinner? That is to say, since I'm currently using an any width and any height storyboard, it's wider than, say, an iPhone 6. So if I set a less than or equal to width constraint on the current width of the field, it seems possible that the field can still expand on a smaller device and not break that constraint.

Is there a better way to do such a width constraint? If not, how else can I keep the field from expanding and pushing the button offscreen?

Technicolor
  • 1,589
  • 2
  • 17
  • 31
  • I've got an answer for you but I can't post now since there's a lot of morons downvoting IOS stuff they don't understand, if you don't find an answer in the next 6 hours, I'll post it. Good luck. – Larry Pickles Aug 17 '15 at 23:57
  • "The problem is that this causes the field to expand horizontally if the text is long enough, sometimes pushing the button off-screen" You need a trailing constraint from the button to its superview, to prevent that from happening. Basically it sounds like you have insufficient constraints in your interface. You need enough constraints to determine the size (height/width) and position (x/y) of _every view_. – matt Aug 18 '15 at 00:02
  • I do have a trailing constraint from the button to its superview. Sorry for not mentioning it. – Technicolor Aug 18 '15 at 00:06

3 Answers3

18

Here's the problem. The text field has a tendency to size itself horizontally to its contents. The button has a tendency to size itself horizontally to its contents. Thus you have an ambiguity: those tendencies have the same priority, so the runtime doesn't know which one to collapse as the text field's text gets too long.

The solution is to adjust those priorities. You must lower the horizontal compression and hugging priorities for the text field - one point lower should be sufficient. Now the button will have a stronger tendency to match its own size to its contents, and the text field will clip its contents.

enter image description here

matt
  • 515,959
  • 87
  • 875
  • 1,141
11

You can also lower the Content Compression Resistance programmatically (this also works if you are using a UIViewRepresentable in SwiftUi):

uiTextField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)

For more info on this topic please refer to:

https://medium.com/@dineshk1389/content-hugging-and-compression-resistance-in-ios-35a0e8f19118

Rob
  • 2,243
  • 4
  • 29
  • 40
4

Selecting the Text view, then within Size inspector: 1) Set "Layout Margins" to "Fixed". 2) Under "Content Compression Resistance Priority", set the "Horizontal" to be "Low (250)".

Claytog
  • 618
  • 8
  • 8
  • 2
    Lowering the Content Compression Resistance Priority makes sense, but why do you set the Layout Margins to by Fixed? I was able to get it to work without setting the Layout Margins to Fixed.... – Microbob Nov 23 '18 at 21:27