How to convert navigationcontroller's back button into browser's back button in iPhone?
I am showing a some text and images in UIwebview in navigation based application. It also contains some link to the source of the data i.e the 开发者_C百科websites. It is showing it in perfect manner. But when user click on the back button it pop the previous viewcontroller and goes to it. But, What I want when user click on the link the back button must be converted into browser's back button and act like it.
Any suggestions/sample code or tutorial for it?
You can replace the navigation bar's back button with a browser back button whenever the UIWebView
has the option to go back by doing something like this:
- (void)updateBackButton {
if ([self.webView canGoBack]) {
if (!self.navigationItem.leftBarButtonItem) {
[self.navigationItem setHidesBackButton:YES animated:YES];
UIBarButtonItem *backItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backWasClicked:)] autorelease];
[self.navigationItem setLeftBarButtonItem:backItem animated:YES];
}
}
else {
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
[self.navigationItem setHidesBackButton:NO animated:YES];
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[self updateBackButton];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self updateBackButton];
}
- (void)backWasClicked:(id)sender {
if ([self.webView canGoBack]) {
[self.webView goBack];
}
}
See First thing that you have to do is, that you have to hide the default back button, as follows below
self.navigationItem.hidesBackButton = YES;
After this you have to add a custom back button on the navigationbar as below:
UIButton *m_BackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
m_BackBtn.frame = CGRectMake(4.0, 5.0+0.0, 100, 30);
[m_BackBtn setTitle:@"Back" forState:UIControlStateNormal];
[m_BackBtn addTarget:selfaction:@selector(BackButtonAction)forControlEvents:UIControlEventTouchUpInside];
[m_BackBtn retain];
[self.navigationController.navigationBar addSubview:m_BackBtn];
*And in the BackButtonAction Function *
-(void)BackButtonAction
{
[yourwebView goBack];
// Do your stuff as you required
}
Take a UIBarbuttonItem from XIB at the place of back button and you can write following in its IBAction method Suppose your UIWebview object is
UIWebView *web;
-(IBAction)goBack
{
[web goBack];
}
goBack is inbuilt method of UIWebView
I like @cduhn's approach above for it's simplicity, but it has the downside of losing the "<" on the Back button. See this answer if you want to keep the "real" back button (with it's "<" icon), but change the button's behavior:
https://stackoverflow.com/a/19132881/132542
For the web view history case here, this is the code I used in my delegate method:
-(BOOL) navigationShouldPopOnBackButton {
if (webView.canGoBack) {
// Then just go back in the web view
[webView goBack];
// return NO to keep the current view controller loaded
return NO;
}
else {
// no web history, so pop the view
return YES;
}
}
This is a bit tricky solution, may not be suggested. But this way you can maintain the back navigation animation on navigation bar.
var urlRequest: NSURLRequest?
////////
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == .LinkClicked {
//AViewController creates and pushes itself to the navigationController
let pushViewController = self.storyboard?.instantiateViewControllerWithIdentifier("AViewController") as! AViewController
pushViewController.urlRequest = request
self.navigationController?.pushViewController(pushViewController, animated: true)
return false
} else { //is initial request from push
return true
}
}
精彩评论