0

I am wanting to have two editable TextFields sitting next to each other on an iOS app in an HStack. However, I get an error ("Static member 'leading' cannot be used on instance of type 'HorizontalAlignment'") whenever I put this into my View file:

var body: some View {
    VStack(alignment: .leading) {
        ...
        VStack(alignment: .leading) {
            HStack {
                TextField("\(aCountry.populationTitle)", text: $aCountry.populationTitle)
                TextField("\(aCountry.population)", text: $aCountry.population)
        }...

This also happens if I have one TextField and two or more Text items in the same HStack.

Possible solution or reason as to why this happens? If I remove one of the TextFields or have only one TextField and one Text item in the same HStack, the app runs smoothly (no errors).

Using xcode11.4.

Thanks.

[EDIT]

Body for MasterView.swift:

struct DetailView: View {
@ObservedObject var aCountry: Country
var body: some View {
    VStack(alignment: .leading) {
        Image("\(aCountry.imageName)").resizable().frame(width: 200, height: 200)
        TextField("\(aCountry.name)", text: $aCountry.name)
            .font(.headline)
        TextField("\(aCountry.continent)", text: $aCountry.continent)
            .font(.subheadline)

        VStack(alignment: .leading) {
            HStack {
                TextField("Population:", text: $aCountry.populationTitle)
                TextField("\(aCountry.population)", text: $aCountry.population)
            }
     }}}
CoderHelp
  • 9
  • 5
  • There is nothing bad with `TextField` views in `HStack`. This is the case when compiler cannot detect error place. Would you provide complete this `body` view and `Country` type declaration? – Asperi Apr 15 '20 at 03:55
  • you can have more than one TextField in a HStack. Show us more of your code. From the given snippet it looks like you are missing a } – workingdog support Ukraine Apr 15 '20 at 04:01
  • I've edited the original post with the body view and Country type declaration added (on my code file all colon and brackets and indentation is correct) – CoderHelp Apr 15 '20 at 04:47

1 Answers1

0

you did not provide a Country class, so I created one. The following test works with the TextFields in a HStack and out of it

class Country: ObservableObject {
    @Published var name = "countryx"
    @Published var continent = "continentx"
    @Published var populationTitle = "populationTitlex"
    @Published var population = "populationx"
    @Published var imageName = "imageNamex"
}

struct ContentView: View {

@ObservedObject var aCountry: Country

var body: some View {
    VStack(alignment: .leading) {
        // Image("\(aCountry.imageName)").resizable().frame(width: 200, height: 200)
        TextField("\(aCountry.name)", text: $aCountry.name).font(.headline)
        TextField("\(aCountry.continent)", text: $aCountry.continent).font(.subheadline)
        VStack(alignment: .leading) {
            HStack {
                TextField("Population:", text: $aCountry.populationTitle)
                TextField("\(aCountry.population)", text: $aCountry.population)
            }
        }
    }
}
}