LF-DevJourney
LF-DevJourney

Reputation: 28529

How to make tabbar navigate between webpages with one wkwebview?

I use wkwebview to render the web page, I have three web pages. I want to use tabbar to navigate between these three pages. I feel each wkwebview a page may take too much resource.

So I just want to use only one wkwebview to load the three-page. when touching the tab, the webpage page loaded. i.e. when tab1 clicked, the page1 loaded, tab2 clicked, the page2 loaded, same for tab3 and page3. Now I can just load a page with wkwebview, I'm new to ios.

if tabbar cannot implement this, what should I do?

Upvotes: 2

Views: 1467

Answers (3)

Nirav D
Nirav D

Reputation: 72420

If you want to something like then add UITabbar with 3 tabitem inside your ViewController and then set delegate of it with your ViewController. Now implement didSelect item method of UITabbar and load the url according the tab.

func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    switch item.title! {
    case "First":
        self.webView.load(URLRequest(url: url1))
    case "Second":
        self.webView.load(URLRequest(url: url2))
    default:
        self.webView.load(URLRequest(url: url3))
    }
}

Upvotes: 1

Arash Tabrizian
Arash Tabrizian

Reputation: 146

If you are not using two different scenes, you need to use combination of UIViewController + UIToolbar. Add your two UIBarButtonItem(s) to your toolbar. Set their action to the desired url and you are done.

Upvotes: 1

Chanchal Chauhan
Chanchal Chauhan

Reputation: 1560

You can override method handleNavBackButton() to get callback on back button.

override func handleNavBackButton() {
  if webViewData.canGoBack {
    webViewData.goBack()
  } else {
    _ = self.navigationController?.popViewController(animated: true)
  }
}

Please let me know if it works for you :)

Upvotes: 1

Related Questions