MJWKWebViewController.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // MJWKWebViewController.m
  3. // MJRefreshExample
  4. //
  5. // Created by Frank on 2019/10/29.
  6. // Copyright © 2019 小码哥. All rights reserved.
  7. //
  8. #import "MJWKWebViewController.h"
  9. #import "UIViewController+Example.h"
  10. #import "MJChiBaoZiHeader.h"
  11. #import "MJRefresh.h"
  12. @import WebKit;
  13. @interface MJWKWebViewController ()<WKNavigationDelegate>
  14. @property (strong, nonatomic) WKWebView *webView;
  15. @end
  16. @implementation MJWKWebViewController
  17. #pragma mark - 示例
  18. - (void)example41 {
  19. __weak WKWebView *webView = self.webView;
  20. webView.navigationDelegate = self;
  21. __weak UIScrollView *scrollView = self.webView.scrollView;
  22. // 添加下拉刷新控件
  23. scrollView.mj_header= [MJChiBaoZiHeader headerWithRefreshingBlock:^{
  24. [webView reload];
  25. }];
  26. // 如果是上拉刷新,就以此类推
  27. [scrollView.mj_header beginRefreshing];
  28. }
  29. #pragma mark - webViewDelegate
  30. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
  31. [self.webView.scrollView.mj_header endRefreshing];
  32. }
  33. #pragma mark - 其他
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. self.webView = [[WKWebView alloc] initWithFrame:self.view.frame];
  37. self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  38. [self.view addSubview:self.webView];
  39. CGSize size = self.view.frame.size;
  40. UILabel *warningLabel = [[UILabel alloc] initWithFrame:CGRectMake(size.width - 210, size.height - 120, 200, 50)];
  41. warningLabel.text = @"注意,这不是原生界面,是个网页:http://weibo.com/excepptions";
  42. warningLabel.adjustsFontSizeToFitWidth = YES;
  43. warningLabel.textColor = UIColor.blackColor;
  44. warningLabel.backgroundColor = [UIColor.lightGrayColor colorWithAlphaComponent:0.3];
  45. warningLabel.numberOfLines = 0;
  46. warningLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
  47. [self.view addSubview:warningLabel];
  48. UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(size.width - 210, size.height - 60, 200, 50)];
  49. [backButton setTitle:@"回到上一页" forState:UIControlStateNormal];
  50. [backButton setBackgroundColor:UIColor.redColor];
  51. backButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
  52. [self.view addSubview:backButton];
  53. [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
  54. // 加载页面
  55. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://weibo.com/exceptions"]]];
  56. #pragma clang diagnostic push
  57. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  58. [self performSelector:NSSelectorFromString(self.method) withObject:nil];
  59. #pragma clang diagnostic pop
  60. }
  61. - (void)viewWillAppear:(BOOL)animated
  62. {
  63. [super viewWillAppear:animated];
  64. [self.navigationController setNavigationBarHidden:YES animated:YES];
  65. [self setNeedsStatusBarAppearanceUpdate];
  66. }
  67. - (void)viewWillDisappear:(BOOL)animated
  68. {
  69. [super viewWillDisappear:animated];
  70. [self.navigationController setNavigationBarHidden:NO animated:YES];
  71. }
  72. - (BOOL)prefersStatusBarHidden
  73. {
  74. return YES;
  75. }
  76. - (IBAction)back {
  77. [self.navigationController popViewControllerAnimated:YES];
  78. }
  79. @end