Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

xcode - SwiftUI: Perpetual Diagnostic Error When Building NavigationBar

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?

question from:https://stackoverflow.com/questions/65931940/swiftui-perpetual-diagnostic-error-when-building-navigationbar

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()
            }
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...