Is it possible to handle the back of WebView with the UINavigationController?

Asked 2 years ago, Updated 2 years ago, 114 views

Push the viewController containing webview to the NavigationController, and then
If the screen transition in the webview is stuck, I want the back button to be back in the webview and back to the original screen if it disappears

ios uinavigationcontroller

2022-09-30 19:28

1 Answers

An easy way is to hide the buttons and make your own.

- (void) viewDidLoad{
    superviewDidLoad;
    webView = [[UIWebView alloc] initWithFrame:self.view.frame];
    [webView loadRequest: [NSURLRequestRequestWithURL: [NSURLURLWithString:@"https://www.google.co.jp/"]]];
    [self.view addSubview:webView];
    // Hide
    [self.navigationItem setHidesBackButton:YES animated:YES];
    UIBarButtonItem*back=[[UIBarButtonItem alloc] initWithTitle: @"back" style: UIBarButtonItemStylePlain target:selfaction:@selector(didBackButtonTap:);
    [self.navigationItem setLeftBarButtonItem:back];
}

-(void) didBackButtonTap:(UIBarButtonItem*)item
{
    if(webView.canGoBack){
        webView goBack;
    } else {
        [self.navigationController popViewControllerAnimated: YES];
    }
}

You need to hook the back button to use it as it is.
I had a helpful question about how to do it.

UINavigationController: How to cancel the back button event?


2022-09-30 19:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.