WKWebView.setMagnification (macOS)

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!

Replies

I have the same issue, posted a sample app - https://github.com/slashlos/SimpleViewer, as part of bug filing on this issue.


In my case the magnification / layout skew is when the content contains an AVAsset such as a movie resulting in a little known AVPlayerView object. As I understand it, this 'Player' is loaded to handle such content, and its layout pins it as you see to the top left of the enclosing WKWebView. It seems a new scroll and player view is installed at the WkWebView's super view or window contentview super.


Also the magnificaiton does not affect the WkWebView as it would when the content is say a web page!? In this context only the player view magnification is affected. Auto layouts are still a daunting topic for me but I'd tried to alter / remove the view's constraints to no avail, as it appears the layout issue is with this player view.


Futher there does not appear to be any way to affect the visibility of a newly added scroll view when you resize the window smaller. If you resize the window smaller (not larger then asset content is possible), the player view will be resized, but not the WkWebView, and so a scroll view is created.


You can then manually scroll your content to center, but the resulting scroll bar is not removable nor have I found yet when / where it arrives to make it hidden as I would like. Like others I'm on the hunt for these new views via notifications of thier appearance, but I suspect this will be problematic as things might change.
While I truly appreciate the new WkWebView advances, I'm feeling being hampered in how to improve the presentation. There clearly appears to be a disconnect regarding magnificaiton when an asset's player is involved.


Perhaps a future sample session will detail how to accomplish this, until I'm hoping someone can answer our question.