如何让 WKWebView 像 SFSafariViewController 一样全屏显示?
Posted
技术标签:
【中文标题】如何让 WKWebView 像 SFSafariViewController 一样全屏显示?【英文标题】:How do I make WKWebView full screen like the SFSafariViewController? 【发布时间】:2020-05-13 01:30:56 【问题描述】:我有一个包含 Web url 列表的 TableView。我想打开一个浏览器,导航到所选的 url,但具有一些自定义功能。当我使用SFSafariViewController
时,我得到了全屏浏览器。但是,当我使用 WKWebViewController
时,我会看到您向下滑动以关闭的模式。
如何让WKWebView
全屏显示,而不是像模态一样?
使用SFSafariViewController
:
class ShoppingViewController: UITableViewController
...
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
showRetailerHome(indexPath: indexPath)
func showHome(indexPath: IndexPath)
...
if let url = URL(string: urlString)
let config = SFSafariViewController.Configuration()
let vc = SFSafariViewController(url: url, configuration: config)
present(vc, animated: true)
结果:
使用WKWebViewController
:
class ShoppingViewController: UITableViewController
...
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
showRetailerHome(indexPath: indexPath)
func showHome(indexPath: IndexPath)
let customWebController = WKBrowserController()
present(customWebController, animated: true)
WKBrowserController
:
class WKBrowserController: UIViewController, WKNavigationDelegate
var webView: WKWebView!
override func loadView()
webView = WKWebView()
webView.navigationDelegate = self
self.view = webView
override func viewDidLoad()
super.viewDidLoad()
let url = URL(string: "https://www.amazon.com")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
结果:
【问题讨论】:
【参考方案1】:当您呈现全屏视图控制器时。你可以设置它。全屏
试试:
let customWebController = WKBrowserController()
customWebController.modalPresentationStyle = .fullScreen
present(customWebController, animated: true)
【讨论】:
你把这段代码放在哪里?很抱歉在这里成为一个真正的新手...... 你应该把你想呈现Web浏览器的控制器。例如,您单击一个按钮并希望呈现 web。你把这段代码放在那个动作中【参考方案2】:class WKBrowserController: UIViewController, WKNavigationDelegate
var Activity: UIActivityIndicatorView!
var webView = WKWebView()
override func viewDidLoad()
super.viewDidLoad()
Activity = UIActivityIndicatorView()
Activity.center = view.center
view.addSubview(Activity)
guard let url = URL(string: "your-url-string") else return
webView = WKWebView(frame: self.view.frame)
webView.translatesAutoresizingMaskIntoConstraints = false
webView.isUserInteractionEnabled = true
webView.navigationDelegate = self
view.addSubview(webView)
let request = URLRequest(url: url)
webView.load(request)
webView.addSubview(Activity)
Activity.startAnimating()
webView.navigationDelegate = self
Activity.hidesWhenStopped = true
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
if navigationAction.navigationType == .linkActivated
if let url = navigationAction.request.url,
let host = url.host, !host.hasPrefix("your-url-string"),
UIApplication.shared.canOpenURL(url)
UIApplication.shared.open(url)
print(url)
print("Redirected to browser. No need to open it locally")
decisionHandler(.cancel)
else
//print("Open it locally")
decisionHandler(.allow)
else
//print("not a user click")
decisionHandler(.allow)
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
Activity.stopAnimating()
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error)
Activity.stopAnimating()
【讨论】:
以上是关于如何让 WKWebView 像 SFSafariViewController 一样全屏显示?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 SwiftUI 中为 WKWebView 创建浏览器选项卡