TGWebViewController.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // TGWebViewController.m
  3. // TGWebViewController
  4. //
  5. // Created by 赵群涛 on 2017/9/15.
  6. // Copyright © 2017年 QR. All rights reserved.
  7. //
  8. #import "TGWebViewController.h"
  9. #import "TGWebProgressLayer.h"
  10. #import <WebKit/WebKit.h>
  11. @interface TGWebViewController ()<WKNavigationDelegate>
  12. @property (nonatomic, strong)WKWebView *tgWebView;
  13. @property (nonatomic, strong)TGWebProgressLayer *webProgressLayer;
  14. @end
  15. @implementation TGWebViewController
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. self.view.backgroundColor = [UIColor whiteColor];
  19. self.automaticallyAdjustsScrollViewInsets = NO;
  20. self.navigationItem.title = self.webTitle;
  21. [self setUpUI];
  22. }
  23. - (void)setUpUI {
  24. UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  25. backBtn.frame = CGRectMake(0, 0, 14, 28);
  26. [backBtn setImage:[UIImage imageNamed:@"arrow_left"] forState:UIControlStateNormal];
  27. [backBtn addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
  28. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
  29. self.tgWebView = [[WKWebView alloc] initWithFrame:self.view.bounds];
  30. self.tgWebView.navigationDelegate =self;
  31. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
  32. [self.tgWebView loadRequest:request];
  33. [self.view addSubview:self.tgWebView];
  34. self.webProgressLayer = [[TGWebProgressLayer alloc] init];
  35. self.webProgressLayer.frame = CGRectMake(0, 42, WIDTH, 2);
  36. self.webProgressLayer.strokeColor = self.progressColor.CGColor;
  37. [self.navigationController.navigationBar.layer addSublayer:self.webProgressLayer];
  38. }
  39. - (void)backButton:(UIButton *)btn {
  40. [self.navigationController popViewControllerAnimated:YES];
  41. [self.webProgressLayer removeFromSuperlayer];
  42. }
  43. #pragma mark - UIWebViewDelegate
  44. - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
  45. [self.webProgressLayer tg_startLoad];
  46. }
  47. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
  48. [self.webProgressLayer tg_finishedLoadWithError:nil];
  49. }
  50. - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
  51. [self.webProgressLayer tg_finishedLoadWithError:error];
  52. }
  53. - (void)dealloc {
  54. [self.webProgressLayer tg_closeTimer];
  55. [_webProgressLayer removeFromSuperlayer];
  56. _webProgressLayer = nil;
  57. }
  58. @end