Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI displayModeButtonItem is internally managed

I am getting this message in the console every time I navigate to another screen:

[Assert] displayModeButtonItem is internally managed and not exposed for DoubleColumn style. Returning an empty, disconnected UIBarButtonItem to fulfill the non-null contract.

Currently I have navigation view set up in the entry point of the app like so

NavigationView {
        KeyboardView(matrixVM: matrixVM, isNavigationBarHidden: $isNavigationBarHidden)
            .background(Color("background")
            .edgesIgnoringSafeArea(.all))
            .navigationBarTitle("Workspace")
            .navigationBarHidden(self.isNavigationBarHidden)
            .onAppear {
                self.isNavigationBarHidden = true
        }
    }

And then inside the KeyboardView I have the navigationlink

NavigationLink(destination: NotebookView(isNavigationBarHidden: $isNavigationBarHidden, saved: matrixVM), label: {
                            Text("Notebooks")
                                .font(.system(size: 14, design: .rounded))
                                .fontWeight(.medium)
                                .foregroundColor(Color("text"))
                                .padding(.trailing, 10)
                        })

Inside the NotebookView I have a list of navigation links (each notebook linking to its detail page)

ScrollView(showsIndicators: false) {
                        ForEach(notebooks, id: \.self) { notebook in
                            
                            NavigationLink(destination: ExpandedSnippet(matrixVM: saved ,notebook: notebook)
                                            .navigationBarTitle("Notebook", displayMode: .inline)) {
                                SnippetCard(notebook: notebook, matrixVM: saved)
                                    .frame(width: UIScreen.main.bounds.width)
                            }
                            .padding(.bottom, 30)
                        }
                    }

Everything seems to be working but just a few hours ago it didn't (I was using tabbar and it suddenly started crashing after working for weeks). I feel like it's a bit of a mess and I am doing something wrong. Any idea why? Thanks for the help!

like image 954
Barkin C. Avatar asked Sep 04 '20 11:09

Barkin C.


4 Answers

I am getting this with Xcode 12.1 when running on the iPhone or iPhone simulator but not the iPad simulator.

Attaching this to the NavigationView fixed it for me.

.navigationViewStyle(StackNavigationViewStyle())
like image 164
Scooter Avatar answered Oct 11 '22 11:10

Scooter


Using Xcode 12.1 (12A7403), this appears to be fixed.

The previous answer remains below for posterity.

I ran into this earlier as well using the latest Xcode 12 beta (12A8189).

This provides a simple MVP to demonstrate the problem.

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: DetailView()) {
                    Text("First View")
                }
            }
            .navigationViewStyle(StackNavigationViewStyle())
        }
    }
}

struct DetailView: View {
    var body: some View {
        List {
            NavigationLink(destination: Text("Detail Title")) {
                Text("New View")
            }
        }
        .navigationBarItems(trailing:
                                Button(action: {
                                    print("Clicked")
                                }) {
                                    Image(systemName: "square.and.arrow.up")
                                })
    }
}

The issue comes down to what device I use.

When running this on an iPhone, I will see the message that OP posts.

When running this on an iPad, I will not see any message.

Now, why is it doing this?

For starters, you can look at this Apple reference for the displayModeButtonItem that the assertion references.

Or, check out the screenshots of the above code in action.

iPhone:

enter image description here

iPad:

enter image description here

Notice how the iPad shows the icon for displayModeButtonItem, while the iPhone does not.

Based on this, my take is that Apple made a mistake. Maybe it will get fixed in the next release?

The best thing you can do would be to file a bug.

FWIW, I did look at the release notes and could not find any reference to this.

like image 9
CodeBender Avatar answered Oct 11 '22 10:10

CodeBender


Attaching .navigationViewStyle(StackNavigationViewStyle()) to the NavigationView does indeed silence the error.

Unfortunately, this is not always a desirable solution, and can sometimes introduce another existing bug specific to StackNavigationViewStyle() in which: a selected list row's background color remains gray after navigating back.

like image 7
Brandon C. Avatar answered Oct 11 '22 10:10

Brandon C.


After updating my iOS 13 app to iOS 14, I am seeing this error and my navbar was not working properly.

To fix this error, I just have to do one change:

NavigationView{
// other code
}
.navigationViewStyle(StackNavigationViewStyle())
like image 7
Anthony Puitiza Avatar answered Oct 11 '22 11:10

Anthony Puitiza