博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios -仿微信有多级网页时,显示返回跟关闭按钮
阅读量:4915 次
发布时间:2019-06-11

本文共 4044 字,大约阅读时间需要 13 分钟。

 

@property (nonatomic, copy) NSString * url;

 

@interface WebViewController ()<UIWebViewDelegate,UIGestureRecognizerDelegate>

 

@property (nonatomic, weak) UIWebView * webView;

 

@property (nonatomic, weak) UIButton * backItem;

@property (nonatomic, weak) UIButton * closeItem;

 

@property (nonatomic, weak) UIActivityIndicatorView * activityView;

 

@end

 

@implementation WebViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    

    [self initNaviBar];

    

    [self initWebView];

    

}

 

- (void)initWebView{

    

    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    webView.delegate = self;

    [self.view addSubview:webView];

    self.webView = webView;

    

    

    //activityView

    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

    activityView.center = self.view.center;

    [activityView startAnimating];

    self.activityView = activityView;

    [self.view addSubview:activityView];

    

    //清除UIWebView的缓存

    [[NSURLCache sharedURLCache] removeAllCachedResponses];

    

    

    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0]];

}

 

- (void)initNaviBar{

    

    UIView * backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];

    backView.backgroundColor = [UIColor purpleColor];

    UIButton * backItem = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 56, 44)];

    [backItem setImage:[UIImage imageNamed:@"back_arrow"] forState:UIControlStateNormal];

    [backItem setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];

    [backItem setTitle:@"返回" forState:UIControlStateNormal];

    [backItem setTitleEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];

    [backItem setTitleColor:[UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000] forState:UIControlStateNormal];

    [backItem addTarget:self action:@selector(clickedBackItem:) forControlEvents:UIControlEventTouchUpInside];

    self.backItem = backItem;

    [backView addSubview:backItem];

    

    UIButton * closeItem = [[UIButton alloc]initWithFrame:CGRectMake(44+12, 0, 44, 44)];

    [closeItem setTitle:@"关闭" forState:UIControlStateNormal];

    [closeItem setTitleColor:[UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000] forState:UIControlStateNormal];

    [closeItem addTarget:self action:@selector(clickedCloseItem:) forControlEvents:UIControlEventTouchUpInside];

    closeItem.hidden = YES;

    self.closeItem = closeItem;

    [backView addSubview:closeItem];

    

    UIBarButtonItem * leftItemBar = [[UIBarButtonItem alloc]initWithCustomView:backView];

    self.navigationItem.leftBarButtonItem = leftItemBar;

    self.navigationController.interactivePopGestureRecognizer.enabled = YES;

    self.navigationController.interactivePopGestureRecognizer.delegate = self;

}

 

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

{

    if (self.webView.canGoBack) {

        [self.webView goBack];

        self.closeItem.hidden = NO;

        return NO;

    }

    return YES;

}

 

#pragma mark - clickedBackItem

- (void)clickedBackItem:(UIBarButtonItem *)btn{

    if (self.webView.canGoBack) {

        [self.webView goBack];

        self.closeItem.hidden = NO;

    }else{

        [self clickedCloseItem:nil];

    }

}

 

#pragma mark - clickedCloseItem

- (void)clickedCloseItem:(UIButton *)btn{

    [self.navigationController popViewControllerAnimated:YES];

}

 

#pragma mark - UIWebViewDelegate

- (void)webViewDidStartLoad:(UIWebView *)webView{

    self.activityView.hidden = NO;

}

 

 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    

    

    NSLog(@"url: %@", request.URL.absoluteURL.description);

    

    if (self.webView.canGoBack) {

        self.closeItem.hidden = NO;

    }

    return YES;

}

 

 

- (void)webViewDidFinishLoad:(UIWebView *)webView{

    self.activityView.hidden = YES;

    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];

}

 

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

    self.activityView.hidden = YES;

}

转载于:https://www.cnblogs.com/shenlaiyaoshi/p/8493720.html

你可能感兴趣的文章
NUMBER类型细讲
查看>>
koa2-3
查看>>
MySQL慢查询日志总结
查看>>
ipad常见错误
查看>>
时钟效果
查看>>
Linux下安装与配置Nginx
查看>>
FCC 基础JavaScript 练习7
查看>>
真的要听妈妈的话。
查看>>
bzoj4873: [Shoi2017]寿司餐厅
查看>>
结对-航空购票系统-开发过程
查看>>
分支语句
查看>>
VBA语句 - 判断语句
查看>>
Android开发代码规范(转)
查看>>
生成函数学习笔记
查看>>
localtime函数时区问题
查看>>
MYSQL分页limit速度太慢优化方法
查看>>
鼎捷智能制造方案 从经营视角实现智能化运营
查看>>
[LeetCode 112 113] - 路径和I & II (Path Sum I & II)
查看>>
关于SpringCloud微服务架构概念的一点理解
查看>>
显示文件后缀
查看>>