确实出现了 UIWebView 重绘视图,我该如何防止这种情况发生?

UIWebView redraws on view did appear, how can I prevent this?

我正在使用 UIWebView 开发应用程序,现在遇到一个问题 如下所列 请帮我解决问题。

有什么办法可以避免这种情况吗?我的 html 内容没有改变,所以我可以将其设置为固定内容或其他内容以更快地绘制它吗?

这是我在网络视图中设置 html 的方式:

webView.loadHTMLString(htmlData, baseURL: nil)

你的 HTMl 代码没有改变,所以把你的 webView 加载代码放在 viewDidLoad 而不是 viewDidAppear 里面,因为 viewDidAppear 总是调用你的 view 出现了,当你的 view 加载时 viewDidLoad 被调用了一次。

您可以在应用程序的生命周期中执行一次引导webview的代码或相应地自定义。

// if you are navigating your application using navigation controller enables you to come back to the rootview without executing whole code of that class associated with view.
// this doesn't apply the whole life cycle of view controller
[self.navigationController pushViewController:vc animated:YES];

// if you navigating through below code this apply the whole life cycle concept of view controller.
[self presentViewController:vc animated:NO completion:nil];

you must have a look about the life cycle of view controller here is a useful apple doc

viewController生命周期短文

  • ViewDidLoad - 当您创建 class 并从 xib 加载时调用。非常适合初始设置和一次性工作。

  • ViewWillAppear - 在您的视图出现之前调用,适用于 hiding/showing 字段或您希望每次在视图可见之前发生的任何操作。因为您可能会在视图之间来回切换,所以每次您的视图即将出现在屏幕上时都会调用此方法。

  • ViewDidAppear - 在视图出现后调用 - 启动动画或从 API.

  • 加载外部数据的好地方
  • ViewWillDisappear/DidDisappear - 与 ViewWillAppear/ViewDidAppear 相同的想法。

  • ViewDidUnload/ViewDidDispose - 在 Objective C 中,这是你清理和释放东西的地方,但这是自动处理的,所以你真正需要做的并不多这里.