UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Why does NavigationView render with a different style?

Forums > SwiftUI

Bit of a SwiftUI neophyte here, purely trying to understand why certain code causes different results! (FWIW, I'm using the new Xcode 12 beta 4.)

I have a (sample) list of dice wrapped in a NavigationView:

struct DieList: View {
    var body: some View {
        NavigationView {
            List {
                Text("Die #1")
                Text("Die #2")
            }
            .navigationBarTitle("Dice")
            //.navigationBarItems(leading: EditButton())
        }
    }
}

And this causes a fairly predictable list with items and a title as expected:

BUT if I uncomment the line of code to create an EditButton in the .navigationBarItems() modifier, suddenly the list looks somewhat different:

Sure, the EditButton shows up, but now there's a grey background, the list items appear with rounded corners, and the lines separating them oddly aren't visible for part of the beginning of each row (look at the whitespace between the dice).

So why does this visual behavior occur?

3      

...

struct EditButtonView: View {
    @State
    private var collection = [0, 1, 2, 3, 4, 5, 6, 7]
    var body: some View {
        NavigationView {
            List {
                ForEach(collection, id: \.self) {
                    Text("Item #\($0)")
                }
                .onDelete(perform: delete)
            }
            .listStyle(InsetListStyle())
            .navigationBarItems(
                trailing: EditButton())
            .navigationBarTitle("Items")
        }
    }
}

extension EditButtonView {
    private func delete(at offsets: IndexSet) {
        collection.remove(atOffsets: offsets)
    }
}

...

3      

Thank you, @DimNovo! I was unaware of the .listStyle() modifier. I appreciate the quick response!

3      

No problem logankriete, it was easy... ))

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.