'Dismissing WebView with custom navigation bar

I have this code:

ForEach(news){ item in
                NavigationLink {
                    WebView(url: URL(string: item.url)!)
                        .navigationOverride(title: .latestNewsTitle) {
                            presentationMode.wrappedValue.dismiss()
                        }
                } label: {
                    LatestNewsListItemView(item: item, geo: geo)
                        .padding([.leading, .trailing])
                }
            }

When cell is clicked, WebView is opened. I wanted to edit WebView navigation bar, so I made this code (I also added this as modifier on WebView in code above).

func navigationOverride(title: String, actionBack: @escaping () -> Void) -> some View {
    
    navigationBarBackButtonHidden(true)
        .navigationBarItems(
            leading: Button(action: actionBack) {
                Image(systemName: "chevron.backward")
                    .frame(width: 50, alignment: .leading)
                    .offset(x: 20)
                
                Text(title)
                    .commonFont(.semiBold, style: .title1)
            }
                .foregroundColor(Color.black)
        )
}

And this is WebView

struct WebView: UIViewRepresentable {

let url: URL
var webview: WKWebView?

func makeUIView(context: Context) -> WKWebView {
    return WKWebView()
}

func updateUIView(_ webView: WKWebView, context: Context) {
    let request = URLRequest(url: url)
    webView.load(request)
}
}

Image and Text in WebView are shown, but the problem is when Button is pressed, it doesnt dismiss WebView.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source