Hello,
I am building a macOS application involving WKWebView, and am having trouble keeping web content centered after a setMagnification operation.
My initial load sets magnification nicely and content is centered and looks great:
extension ViewController : WKNavigationDelegate {
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
let scale = view.bounds.size.width / webView.bounds.size.width
self.magnification = scale
webView.setMagnification(self.magnification, centeredAt: centerPoint())
}
}
However when I use a button to zoom out, the content gets pinned to the upper left of the WKWebView:
func centerPoint() -> CGPoint {
return CGPoint( x:(view.frame.origin.x + (view.frame.size.width / 2) ),
y:(view.frame.origin.y + (view.frame.size.height / 2)))
}
@IBAction func zoomOutButtonPressed(_ b:NSButton ){
self.magnification -= 0.1
self.webView.setMagnification(self.magnification, centeredAt: self.centerPoint())
}
Is there anyway to keep this webview content centered horizontally (without resorting to shifting webview.frame.origin)?
Thank you!