WKWebView 'allowsMagnification' Deprecated: Pinch-to-Zoom Solution
The error message 'Value of type 'WKWebView' has no member 'allowsMagnification'' indicates that the property 'allowsMagnification' is no longer available in WKWebView starting from iOS 14. It has been deprecated.
If you need to enable or disable pinch-to-zoom functionality in WKWebView, you can achieve this by utilizing the 'scrollView' property of WKWebView and setting its 'delegate' property to your view controller. Subsequently, implement the UIScrollViewDelegate methods within your view controller to manage the zoom behavior.
Here's an example of how you can enable or disable pinch-to-zoom in WKWebView:
import UIKit
import WebKit
class ViewController: UIViewController, UIScrollViewDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let configuration = WKWebViewConfiguration()
webView = WKWebView(frame: view.bounds, configuration: configuration)
webView.scrollView.delegate = self
view.addSubview(webView)
// Load your web content
let url = URL(string: 'https://www.example.com')
let request = URLRequest(url: url!)
webView.load(request)
}
// MARK: UIScrollViewDelegate
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return nil // returning nil disables pinch-to-zoom
// return scrollView.subviews.first // returning the web view allows pinch-to-zoom
}
}
In the 'viewForZooming' method, you can return 'nil' to disable pinch-to-zoom, or you can return the web view itself (scrollView.subviews.first) to enable pinch-to-zoom.
Remember to set the class of your view controller to ViewController in your storyboard or programmatically create an instance of ViewController and present it.
原文地址: http://www.cveoy.top/t/topic/dTT5 著作权归作者所有。请勿转载和采集!