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

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

Ok, it works, thanks a lot. But I forgot to link an image: imgur.com/8VIJ7Wy
Empty space I meant before the navigationbartitle and the navigation link to go back
@MarcoGT try putting the .navigationBarTitle("Aggiungi Dispensa", displayMode: .inline) inside the NavigationLink of the previous View
@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.
@aheze, sorry, I don't get it, what do you mean? I do not have any NavigationLink
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.