Swift - back button in UIWebview with navigation button NavigationBar

Currently, I am using a button on my uiwebview to return through every web page that the user is looking for and works fine. But is there anyway to use the back button in the navigation bar to go back to previous web pages, and when you no longer have pages to go back to open the previous view controller?

It uses the im code used for the return that is just connected to UIButton.

// Back button events func onBackButton_Clicked(sender: UIButton) { if(webview.canGoBack) { webview.goBack() } } 

Any help would be great.

+5
source share
3 answers

Adding to your code, you could add another that will work when there is no more web search history, and pull out the view controller, which will return to the previous view controller (if you see the controllers in the navigation controller):

 if(webview.canGoBack) { //Go back in webview history webview.goBack() } else { //Pop view controller to preview view controller self.navigationController?.popViewControllerAnimated(true) } 

Also, if you want to remove the default return button and use your custom button, add this code to ViewDidLoad:

 self.navigationItem.backBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: self, action: "onBackButton_Clicked:") 

And change your method above:

 onBackButton_Clicked(sender: UIButton) 

in

 onBackButton_Clicked(sender: UIBarButtonItem) 
+12
source

I would suggest overloading the function of the back button and checking the history.

 override func viewDidLoad { super.viewDidLoad() self.navigationItem.hidesBackButton = true let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Bordered, target: self, action: "back:") self.navigationItem.leftBarButtonItem = newBackButton; } func back(sender: UIBarButtonItem) { if(webview.canGoBack) { webview.goBack() } else { self.navigationController.popViewController(animated:true) } } 
+4
source

You need to use the BackBarButton for 1. Go back through the previous web pages 2. Return to the previous controller from the first loaded web page. The latter requirement seems to be satisfied by BackButton on NavigationBar at the moment. What you need to do is add a custom BackButton .

 override func viewDidLoad() { super.viewDidLoad() leftButton = UIButton(type: UIButtonType.Custom) leftButton.frame = CGRectMake(0, 0, 36, 36) leftButton.clipsToBounds = true leftButton.setImage(UIImage(named: "yourBackButton.png"), forState: .Normal) // add custom image leftButton.addTarget(self, action: "onBackButton_Clicked:", forControlEvents: UIControlEvents.TouchUpInside) let leftBarButton = UIBarButtonItem() leftBarButton.customView = leftButton self.navigationItem.leftBarButtonItem = leftBarButton } func onBackButton_Clicked(sender: UIButton) { if(webview.canGoBack) { webview.goBack() } else { self.navigationController.popViewController(animated: true) } } 
0
source

Source: https://habr.com/ru/post/1246217/


All Articles