1

I am a beginner with SwiftUI Layout (not in coding) and I have a problem

enter image description here

Here the code:

var body: some View {
    NavigationView {
        VStack {
            Form {
                TextField("Nome", text: $name)
            }
            .navigationBarTitle("Aggiungi Dispensa", displayMode: .inline)
            .navigationBarItems(trailing: Button("Aggiungi") {
                if self.name.count > 0 {
                    let item = DispItem(name: self.name)
                    self.dispense.items.append(item)
                }
                self.name = ""
            })
            
            List {
                let sortedItems = dispense.items.sorted {
                    $0.name < $1.name
                }
                ForEach(sortedItems) { item in
                            Text(item.name)
                                .font(.headline)
                }
                .onDelete(perform: removeItems)
            }
        }
    }
}

Basically is a VStack composed by Form and List But I do not understand where the empty space at the top comes from and the white space between Form and List.

Thanks Marco

koen
  • 5,383
  • 7
  • 50
  • 89
MarcoGT
  • 53
  • 6
  • Does this answer your question https://stackoverflow.com/a/62382165/12299030? And next about empty space https://stackoverflow.com/a/64838243/12299030. – Asperi Feb 18 '21 at 15:56

1 Answers1

1

As @Asperi points out in the links in his comment, you can use VStack(spacing: 0) to remove the space between the Form and List.

For the space between the navigation bar link and title, it looks like you are nesting NavigationView items inside each other.

The view that calls this view (is it called "Impostazione"?) has a NavigationView so your child view ("Aggiungi Dispensa") doesn't need its own NavigationView

John Nimis
  • 586
  • 3
  • 15
  • Ok, it works, thanks a lot. But I forgot to link an image: https://imgur.com/8VIJ7Wy – MarcoGT Feb 18 '21 at 16:41
  • Empty space I meant before the navigationbartitle and the navigation link to go back – MarcoGT Feb 18 '21 at 16:45
  • @MarcoGT try putting the `.navigationBarTitle("Aggiungi Dispensa", displayMode: .inline)` inside the NavigationLink of the previous View – aheze Feb 18 '21 at 16:51
  • 1
    @MarcoGT I updated my answer (again) - you are nesting `NavigationView` items. If you had a `NavigationView` in the view before this one, you don't need another one. – John Nimis Feb 18 '21 at 17:07
  • @aheze, sorry, I don't get it, what do you mean? I do not have any NavigationLink – MarcoGT Feb 18 '21 at 19:20
  • @MarcoGT in the parent view (the view before this one - ImpostazioniView). @JohnNimis is right, you won't need to have another `NavigationView`. – aheze Feb 18 '21 at 19:26