有这样一个页面 加载了一个网页 网页自带导航栏 这时会与系统导航栏重复 这里简单地进行处理 去掉网页的导航栏 使用系统自带的导航栏实现返回功能 标题的显示
- (void)viewDidLoad {
[superviewDidLoad];
/// 隐藏系统返回按钮 进行自定义
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItemalloc]initWithImage:[UIImageimageNamed:@"backicon"]style:UIBarButtonItemStylePlaintarget:selfaction:@selector(backacrion)];
[selfsetupView];
}
- (void)setupView {
/// 添加加载菊花
self.hud = [MBProgressHUDshowHUDAddedTo:self.navigationController.viewanimated:YES];
self.hud.label.textColor = fontColor;
self.hud.label.font = [UIFont systemFontOfSize:15];
self.hud.mode = MBProgressHUDModeIndeterminate;
//创建webview
_webView = [[UIWebViewalloc]initWithFrame:CGRectMake(0,0,appWidth,appHeight -64)];
[_webViewloadRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:self.url]]];
_webView.delegate =self;
[self.viewaddSubview:_webView];
}
- (void)backacrion{
if ([self.webViewcanGoBack]) {
/// 网页可以返回 就进行网页返回
[self.webViewgoBack];
}else{
[self.viewresignFirstResponder];
/// 网页返回到首页了 返回不了了 这时候我们的控制器返回
[self.navigationControllerpopViewControllerAnimated:YES];
}
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
self.hud.hidden =NO;
self.webView.hidden =YES;
returnYES;
}
在网页加载结束的时候获取头标签进行去除操作
使用谷歌浏览器的开发者工具查看网页源代码 寻找头标签
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// 1.获取页面标题
NSString *string =@"document.title";
//获取当前页面的title
设置导航栏标题
NSString * title = [webViewstringByEvaluatingJavaScriptFromString:@"document.title"];
self.title = title;
[webView stringByEvaluatingJavaScriptFromString:string];
// 2.去掉页面标题
NSMutableString *str = [NSMutableStringstring];
// 3.根据标签类型获取指定标签的元素
[str appendString:@"var header = document.getElementsByTagName(\"header\")[0];"];
[str appendString:@"header.parentNode.removeChild(header);"];//移除头部的导航栏
[webView stringByEvaluatingJavaScriptFromString:str];
[selfperformSelector:@selector(hidenaction)withObject:selfafterDelay:0.1];
}
- (void)hidenaction{
/// 开始加载时隐藏webview 加载完后显示,原因是 因为我们要去掉头标签,,去掉的方法是在网页加载完毕进行的,,添加一个延时现实的方法 可以隐藏掉网页先显示头标签又被移除的过程。使其看起来更自然一些
self.webView.hidden =NO;
self.hud.hidden =YES;
}