HomeiOS DevelopmentIs there a solution to get a TextField to dimension to suit...

Is there a solution to get a TextField to dimension to suit so I can put a button straight subsequent to it?


In my app, I’ve a TextField {that a} person can enter a number of traces of textual content into. I would 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 power the TextField to take up as a lot house as doable horizontally i.e:

        HStack
        {
            TextField("", textual content: self.$textFieldUserInput, axis: .vertical).lineLimit(1...).targeted(self.$textFieldIsFocused)
            Button(""){}.buttonStyle(.borderedProminent)
            Spacer()
        }

will result in:

Is there a solution to get a TextField to dimension to suit so I can put a button straight subsequent to it?

After I’m wanting this:

enter image description here

I’ve provide you with a good work round that replaces the TextField when it isn’t targeted with a Textual content part:

        ZStack(alignment: .main)
        {
            TextField("", textual content: self.$textFieldUserInput, axis: .vertical)
            .lineLimit(1...)
            .targeted(self.$textFieldIsFocused)
            .opacity(self.textFieldIsFocused ? 1 : 0)

            if (!self.textFieldIsFocused)
            {
                HStack
                {
                    Textual content(self.textFieldUserInput)
                    .onTapGesture
                    {
                        self.textFieldIsFocused = true
                    }
                    
                    Button(""){}.buttonStyle(.borderedProminent)
                }
            }
        }

The disadvantage to that is that now you must faucet the Textual content part to activate the TextField so the person cannot particularly resolve the place they need the cursor to begin (and likewise when the TextField is energetic you both need to make the Button disappear or dwell with it being shifted all the best way to the precise once more).

Am questioning if there’s a higher solution to accomplish this, to in some way make a TextField in SwiftUI dimension to suit so you’ll be able to place a button straight subsequent to it?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments