I want to give action to button... in swiftui

Here i need help, i want to change webview by clicking a button in navigation bar item please help me, can be this possible... IN swiftui


Import Swiftui



var body: some View {




NavigationView {

VStack {

WebKit(request: URLRequest(url: URL(string : api + endpoint)!))

}.navigationBarTitle(Text(""), displayMode: .inline)

.navigationBarItems(leading: HStack{

Button(action:{}){

Image("test2")

.foregroundColor(Color.black)

};


};Button(action:{

}){

Image("test2")

.foregroundColor(Color.black)

.padding(.top, 5.0)

}},

trailing: HStack{ Button(action:{}){

Image("test2")

.foregroundColor(Color.black)

};

Button(action:{}){

Image("test2")

.foregroundColor(Color.black)

};

Button(action:{}){

Image("test2")

.foregroundColor(Color.black)

}



}

) }

};func onAppear(){

UINavigationBar.appearance().barTintColor = UIColor(red: 0, green: 100/255, blue: 205/255, alpha: 1)

UINavigationBar.appearance().tintColor = UIColor.white

UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]

}



}



struct WebKit: UIViewRepresentable{


let request: URLRequest

func makeUIView(context: UIViewRepresentableContext<WebKit>) -> WKWebView {

return WKWebView()

}



func updateUIView(_ uiView: WKWebView, context: Context) {

uiView.load(request)

}




}

Replies

In SwifUI … So please move the thread to SwiftUI section.


And format the code with tool formatter (<>) and eliminating all the extra blank lines.


Your code as published cannot compile:

- import Swiftui is eeroneous : import SwiftUI

- };func onAppear(){ should cause a compiler error.



So please take care to publish real code and take time to present it correctly.


have a good day.

ok so this is real code please help me with this


import SwiftUI

import WebKit

import UIKit



struct ContentView: View {

@State var showMenu = false

@State private var selection: Int? = nil

@State var refresh: Bool = false

var api = "https://google.com/"

let endpoint = ""


var body: some View {


NavigationView {

VStack {

WebView(request: URLRequest(url: URL(string : self.api + self.endpoint)!))

}.navigationBarTitle(Text(""), displayMode: .inline)

.navigationBarItems(leading: HStack{ Button(action:{

}){

Image("test2")

.foregroundColor(Color.black)

.padding(.top, 5.0)

.animation(.linear)

}},

trailing: HStack{ Button(action:{}){

Image("test2")

.foregroundColor(Color.black)

};

Button(action:{}){

Image("test2")

.foregroundColor(Color.black)

};

NavigationLink(destination: testview()) {

Text("")

Image("test2")

}


}

) }

}

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

if let url = navigationAction.request.url {

if url.host == "www.apple.com" {

UIApplication.shared.open(url)

decisionHandler(.cancel)

return

}

}

;func onAppear(){

UINavigationBar.appearance().barTintColor = UIColor(red: 0, green: 100/255, blue: 205/255, alpha: 1)

UINavigationBar.appearance().tintColor = UIColor.white

UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]

}


}



struct WebView : UIViewRepresentable {


// static is only for demo, this can be some external model

static var cache = [URL: WKWebView]()


let request: URLRequest


func makeUIView(context: Context) -> WKWebView {

guard let url = request.url else { fatalError() }


if let webView = WebView.cache[url] {

return webView

}


let webView = WKWebView()

WebView.cache[url] = webView

return webView

}


func updateUIView(_ uiView: WKWebView, context: Context) {

if uiView.url == nil {

uiView.load(request)

}

}

}




so this is it please let me know how to create a weburl within a button

I don’t understand the question. “How to create a weburl within a button”?? Do you mean that when you click/tap a button, it should open a specific url in the web view?

What about:


And format the code with tool formatter (<>) and eliminating all the extra blank lines.