In my app, I’ve a TextField {that a} person can enter a number of traces of textual content into. I might like to have the ability to place a Button proper subsequent to the TextField, however am unable to do that as a result of SwiftUI will drive the TextField to take up as a lot house as doable horizontally i.e:
HStack
{
TextField("", textual content: self.$textFieldUserInput, axis: .vertical).lineLimit(1...).centered(self.$textFieldIsFocused)
Button(""){}.buttonStyle(.borderedProminent)
Spacer()
}
will result in:
Once I’m wanting this:
I’ve provide you with a good work round that replaces the TextField when it is not centered with a Textual content element:
ZStack(alignment: .main)
{
TextField("", textual content: self.$textFieldUserInput, axis: .vertical)
.lineLimit(1...)
.centered(self.$textFieldIsFocused)
.opacity(self.textFieldIsFocused ? 1 : 0)
if (!self.textFieldIsFocused)
{
HStack
{
Textual content(self.textFieldUserInput)
.onTapGesture
{
self.textFieldIsFocused = true
}
Button(""){}.buttonStyle(.borderedProminent)
}
}
}
The downside to that is that now it’s important to faucet the Textual content element to activate the TextField so the person cannot particularly determine the place they need the cursor to start out (and likewise when the TextField is lively you both should make the Button disappear or stay with it being shifted all the best way to the fitting once more).
Edit: I’ve tried many of the options (SwiftUI TextField takes max width and Easy methods to dynamically regulate the width of a TextField to its content material?) that contain utilizing fixedSize, however that does not work while you need multiline textual content and have to make use of axis: .vertical within the TextField.
Am questioning if there’s a higher approach to accomplish this, to someway make a TextField in SwiftUI measurement to suit so you may place a button instantly subsequent to it?

![Is there a approach to get a multiline TextField to measurement to suit so I can put a button instantly subsequent to it? [duplicate] Is there a approach to get a multiline TextField to measurement to suit so I can put a button instantly subsequent to it? [duplicate]](https://i.sstatic.net/pzl8k13f.png)

