1

I'm new to SwiftUI, and I'm trying to build this nav bar using Xcode 12.4:

enter image description here

Here is the entirety of my view:

struct PreferencesView: View {
  var body: some View {
    NavigationView {
      ZStack {
        //Background Color
        Color("DosDark")
          .edgesIgnoringSafeArea(.all)
        Text("Hey.")
        //Nav bar styles
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            ToolbarItem(placement: .principal) {
                VStack {
                    Text("Preferences")
                    .navBarTitleDark()
                }
            }
        }
        .navigationBarItems(
          leading: NavClose(), //<-- This is where the trouble starts
          trailing: NavAbout()
        ) 
      }
    }
  }
}

struct NavClose: View {
  var body: some View { //<-- Inexplicable error here
    Button(action: {
      print("Close...")
    }){
      Image("close-blue")
    }
  }
}

struct NavAbout: View {
  var body: some View {
    Button(action: {
      print("Show about stuff...")
    }) {
      Image("about-blue")
    }
  }
}

I can get the title to render okay, but as soon as I add the .navigationBarItems bit, I see an error endlessly on my struct that I'm trying to pull in:

enter image description here

When I try putting the Button directly in .navigationBarItems (without using an external struct) I still see the error on that line:

Failed to produce diagnostic for expression; please file a bug report

Am I doing something wrong? Is there a way to make Xcode give me a real error message?

1 Answer 1

1

Works fine with Xcode 12.1 / iOS 14.1, but .navigationBarItems was deprecated for the preference of toolbar and probably you have newer version where they are already conflicted.

The solution is to use only toolbar with corresponding placements, like

    .toolbar {
        ToolbarItem(placement: .navigationBarLeading) {
                NavClose()
            }
        ToolbarItem(placement: .navigationBarTrailing) {
                NavAbout()
            }
        ToolbarItem(placement: .principal) {
            VStack {
                Text("Preferences")
                .navBarTitleDark()
            }
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks very much for your help! I didn't know about the deprecation of .navigationBarItems (and good riddance). 🙂 It looks like the root of the problem is the Button(). If I replace it with just a Image("close-blue") it works. There's something about the Button closure syntax that it doesn't like. 🤔
Ah, I found the issue. I had a typealias named Button and Xcode was choking on it. D'oh! 🤦🏻‍♂️ Thanks again for your help!

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.