这篇文章主要处理加载多层网页返回的问题,其实功能很简单,自己当时做的时候没带脑子,找了好久资料没有想要的(其实是自己不长心),记录一下.
1.在导航上创建一个关闭按钮的btn,默认alpha =0
UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
[self.nav addSubview:btn];
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.offset(WIDTH_CONSTARIN(60));
make.top.offset(STATUS_BAR_HEIGHT);
make.size.mas_equalTo(CGSizeMake(60, 44));
}];
btn.alpha =0;
[btn setTitle:@"关闭" forState:UIControlStateNormal];
btn.titleLabel.font =FONT(16.f);
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn addTarget:self action:sel_registerName("closeClick") forControlEvents:UIControlEventTouchUpInside];
_closeBtn =btn;
2.处理关闭按钮是否显示(以wkWeb代理为例)
//index不用解释了
//pageBack参数主要是为了放置index在返回时调用didFinishNavigation再次+1的问题,可以理解为是push和pop,
@property (nonatomic, assign) NSInteger index;//记录加载的页数
@property (nonatomic, assign) BOOL pageBack;//判断是否点击了返回
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
if (!_pageBack) {//是新页面 index 就+1
++_index;
}
_pageBack = NO;
if (_index >=2 ) {//页数大于等于2 关闭按钮就显示
_closeBtn.alpha =1;
}else{
_closeBtn.alpha =0;
}
}
3.处理返回和关闭逻辑
//返回操作
- (void)leftViewClick {
_pageBack = YES;//是pop,返回结束后,index不用+1
if (_index >=2) {
_index--;
[self.WKWebView goBack];
}else{//就一页了 可以直接退出去
[self.navigationController popViewControllerAnimated:YES];
}
}
//关闭操作
- (void)closeClick {
[self.loading removeFromSuperview];
[self.navigationController popViewControllerAnimated:YES];
}