zummer
zummer

Reputation: 15

How to remove space between TextField and Text on HStack in SwiftUI?

I have a TextField and Text next in HStack Sections. There is a large empty space between them and I can not figure out how to remove it.

VStack{

    Section {
        HStack {
            TextField("Kohalik raha", text: $localMoney)
                .multilineTextAlignment(.center)
            Text("GEL")
                .multilineTextAlignment(.leading)
        }
    }
    Section {
        Text("\(money, specifier: "%.2f") EUR")
    }
    Section {
        HStack {
            TextField("Kurss", text: $rate )
                .multilineTextAlignment(.center)
            Text("Kurss")
                .multilineTextAlignment(.leading)
        }
    }
}

[Picture from app]

Upvotes: 1

Views: 1475

Answers (2)

Mojtaba Hosseini
Mojtaba Hosseini

Reputation: 119184

Make the textField have fixedSize to bring them near in center:

HStack {
    TextField("Kohalik raha", text: $localMoney)
        .background(Color.blue)
        .multilineTextAlignment(.center)
        .fixedSize()

    Text("GEL")
        .multilineTextAlignment(.leading)
        .background(Color.yellow)
}

Preview

I have added some colors to see better ;)

Upvotes: 3

Michael Long
Michael Long

Reputation: 1092

Add a Spacer() after the TextField and Text views.

Upvotes: 0

Related Questions