获取 ios 故事板 webView 应用程序以在安全区域呈现
Posted
技术标签:
【中文标题】获取 ios 故事板 webView 应用程序以在安全区域呈现【英文标题】:Getting an ios storyboard webView app to render in safe area 【发布时间】:2020-09-30 17:40:16 【问题描述】:可能是一个简单的修复,但我正试图让我的简单 ios 故事板应用程序在我的 iPhone 11 上的安全区域中呈现。
它目前占用了整个视图空间,而不是使用安全区域。我不确定是否需要对代码进行任何更改,或者它只是情节提要视图中未启用的设置。
这是我的第一个应用程序。感谢您的帮助。
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate
var webView: WKWebView!
override func viewDidLoad()
super.viewDidLoad()
let myURL = URL(string:"https://google.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
if #available(iOS 11.0, *)
webView.scrollView.contentInsetAdjustmentBehavior = .never;
webView.navigationDelegate = self
webView.allowsBackForwardNavigationGestures = true
override func loadView()
let webConfiguration = WKWebViewConfiguration()
webConfiguration.dataDetectorTypes = [.all]
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
extension ViewController: WKNavigationDelegate
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
guard
let url = navigationAction.request.url else
decisionHandler(.cancel)
return
let string = url.absoluteString
if (string.contains("mailto:"))
UIApplication.shared.open(url, options: [:], completionHandler: nil)
decisionHandler(.cancel)
return
if (string.contains("sms:"))
UIApplication.shared.open(url, options: [:], completionHandler: nil)
decisionHandler(.cancel)
return
decisionHandler(.allow)
class FullScreenWKWebView: WKWebView
override var safeAreaInsets: UIEdgeInsets
return UIEdgeInsets(top: 100, left: 0, bottom: 0, right: 0)
【问题讨论】:
【参考方案1】:这是因为您将 webView 设置为控制器的视图。我认为最简单的方法是将 webView 设置为 viewController 主视图的子视图。首先将初始化代码从loadView
方法移动到viewDidLoad
,将 webView 添加为 subView,最后将 webView 锚点约束到 safeArea 锚点。
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate
var webView: WKWebView!
override func viewDidLoad()
super.viewDidLoad()
let webConfiguration = WKWebViewConfiguration()
webConfiguration.dataDetectorTypes = [.all]
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(webView)
NSLayoutConstraint.activate([
webView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
webView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
webView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
webView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
])
let myURL = URL(string:"https://google.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
if #available(iOS 11.0, *)
webView.scrollView.contentInsetAdjustmentBehavior = .never;
webView.navigationDelegate = self
webView.allowsBackForwardNavigationGestures = true
extension ViewController: WKNavigationDelegate
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
guard
let url = navigationAction.request.url else
decisionHandler(.cancel)
return
let string = url.absoluteString
if (string.contains("mailto:"))
UIApplication.shared.open(url, options: [:], completionHandler: nil)
decisionHandler(.cancel)
return
if (string.contains("sms:"))
UIApplication.shared.open(url, options: [:], completionHandler: nil)
decisionHandler(.cancel)
return
decisionHandler(.allow)
如果前一个选项无效,您可以使用您的代码并使用 viewSafeAreaInsetsDidChange
方法更改 webView 边界,使其了解安全区域插入。只需添加以下代码:
override func viewSafeAreaInsetsDidChange()
super.viewSafeAreaInsetsDidChange()
view.bounds = view.safeAreaLayoutGuide.layoutFrame
【讨论】:
以上是关于获取 ios 故事板 webView 应用程序以在安全区域呈现的主要内容,如果未能解决你的问题,请参考以下文章
在 WebView 加载时显示另一个故事板,然后切换回原始故事板