Tuesday, July 23, 2024
HomeiOS Developmentios - SwiftUI: Navigation bar duplicates after opening exterior hyperlink and returning...

ios – SwiftUI: Navigation bar duplicates after opening exterior hyperlink and returning to app


I am experiencing a difficulty with my SwiftUI app the place the navigation bar duplicates after opening an exterior hyperlink and returning to the app. This occurs each on a bodily iPhone 11 gadget and within the simulator.
Here is a simplified model of my DetailView:

swift
struct DetailView: View {
let merchandise: RSSItem

var physique: some View {
    ScrollView {
        VStack(alignment: .main) {
            Button(motion: {
                openURL(merchandise.url)
            }) {
                Textual content(merchandise.title)
                    .font(.title)
                    .padding(.backside)
            }
            
            Textual content(merchandise.textual content)
                .font(.physique)
            
            // Extra content material...
        }
        .padding()
    }
    .navigationBarTitle(merchandise.title, displayMode: .inline)
}

func openURL(_ urlString: String) {
    guard let url = URL(string: urlString) else {
        print("Invalid URL: (urlString)")
        return
    }
    
    UIApplication.shared.open(url)
}

}

And this is how I am organising the navigation bar within the father or mother view (FirestoreDataView):
swift
struct FirestoreDataView: View {
// … different state variables and properties

var physique: some View {
    NavigationView {
        Checklist {
            // ... checklist content material
        }
        .listStyle(PlainListStyle())
        .navigationBarItems(main: leadingBarItems, trailing: trailingBarItems)
        .navigationBarTitleDisplayMode(.inline)
    }
    .background(Colour.white.edgesIgnoringSafeArea(.all))
    .onAppear {
        fetchData()
        customizeNavigationBar()
    }
}

@ViewBuilder
non-public var leadingBarItems: some View {
    HStack {
        Button("All Learn") {
            markAllAsRead()
        }
        
        Button(showUnreadOnly ? "Unread" : "All") {
            showUnreadOnly.toggle()
        }
    }
}

@ViewBuilder
non-public var trailingBarItems: some View {
    HStack {
        Toggle("KI Solely", isOn: $showKIOnly)
            .toggleStyle(CheckboxToggleStyle())
        
        Toggle("Intestine", isOn: $showGutOnly)
            .toggleStyle(CheckboxToggleStyle())
        
        Picker("Class", choice: $selectedCategory) {
            Textual content("All").tag("")
            ForEach(classes, id: .self) { class in
                Textual content(class).tag(class)
            }
        }
    }
}

func customizeNavigationBar() {
    let look = UINavigationBarAppearance()
    look.configureWithTransparentBackground()
    look.backgroundColor = .clear
    look.titleTextAttributes = [.font: UIFont.systemFont(ofSize: 16)]
    let desiredHeight: CGFloat = 44.0 / 3.0 // Modify the peak as wanted
    UINavigationBar.look().standardAppearance = look
    UINavigationBar.look().compactAppearance = look
    UINavigationBar.look().scrollEdgeAppearance = look
    UINavigationBar.look().body.measurement.top = desiredHeight
}

}

The difficulty happens after I faucet the title button within the DetailView, which opens the URL within the default browser. Once I return to the app, the navigation bar is duplicated, exhibiting all buttons twice.

I’ve tried the next options, however the subject persists:
Eliminated the navigationBarTitle modifier from the VStack and utilized it to the ScrollView as a substitute.
Wrapped the ScrollView with a NavigationView and utilized the navigationBarTitle modifier to it.
Used a ZStack as a substitute of a ScrollView and utilized the navigationBarTitle modifier to the ZStack.
Carried out a customized SafariViewController to open the hyperlink throughout the app.
Added .navigationViewStyle(StackNavigationViewStyle()) to the principle NavigationView within the father or mother view.
Tried utilizing WKWebView as a substitute of UIApplication.shared.open(url) to open the hyperlink throughout the app.
Eliminated the customizeNavigationBar() perform to see if the customized look was inflicting the problem.
None of those options have resolved the problem. The navigation bar nonetheless duplicates when returning to the app after opening an exterior hyperlink.

Extra data:
The app makes use of Firebase Firestore for knowledge storage and retrieval.
The difficulty happens persistently throughout completely different iOS variations (14, 15, and 16).
The issue persists even after cleansing the construct folder and restarting Xcode.
Has anybody encountered an analogous subject or can counsel an answer? Any assist can be drastically appreciated!



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments