Suppress WKWebView from scaling content to render at same magnification as UIWebView does
Asked Answered
A

1

76

Problem

Using WKWebView in place of UIWebView, I noticed that the contents of the WKWebView were massively scaled down as compared to my UIWebView. I'd like the WKWebView to stop doing that, and just respect my CSS values literally, the way UIWebView would.

Context

I use web views in my native iOS app for the contents that go inside popovers on an iPad where I'm displaying informational content. It's great being able to give this to content and design folks and say, "just drop in some HTML content, referencing the linked CSS file".

All of this worked just great when using UIWebView.

But popovers are often quite compact.

What I'm Experiencing

In my narrow popovers, the content is scaled down as if my content where an entire web page being miniaturized to fit. I can see that WKWebView was primarily intended for folks building alternate browsers on iOS or building hybrid apps, where the WKWebView is essentially, taking over the screen.

The magnification factor in WKWebView is not something one can set. Sure, the user can zoom in, but that defeats the purpose. I'm not looking for a zoomed in viewport; I'm looking for the entire content to fit and word-wrap like it would with UIWebView.

With WKWebView, in the simulator, I'll see the sizing I want if I bump up my body text size to 50px instead of 14px. This code however, doesn't even help when on the device, so inflating my CSS sizes is not an option either!

What's Wrong with UIWebView?

Well, nothing really. It's not (yet) deprecated in iOS8, perhaps b/c it has usefulness still, as I'm experiencing. I did notice however, that WKWebView was fast. And I've seen some speculation that the writing is on the wall for UIWebView. I thought: "Why not adopt the more modern API in iOS8 now?"

So, I continue to use UIWebView for now, but I'd like to switch over to WKWebView if I could get it to respect my CSS sizing.

Here's my original CSS that UIWebView renders nicely, but which WKWebView in a popover will render in really tiny fonts:

body {
    font-family: "HelveticaNeue";
    font-size: 15px;
    line-height: 17px;
    color: #000000;
}

h1 {
    font-family: "HelveticaNeue-Bold";
    font-size: 18px;
    line-height: 20px;
    color: #000000;
}


h2 {
    font-family: "HelveticaNeue-Bold";
    font-size: 16px;
    line-height: 18px;
    color: #000000;
}
Avera answered 29/9, 2014 at 14:59 Comment(0)
E
181

I had the same problem. I just had to put

<meta name="viewport" content="initial-scale=1.0" />

into my header block and that solved it for me. Looks like WKWebView behaves more like Mobile Safari than a UIWebView does, so you need to set the viewport if you want to control scaling or general sizing.

Elfin answered 3/11, 2014 at 17:55 Comment(10)
You have saved my life, I was totally depressed as to why it was messing up my CSS, but you saved me. You are my angel!Heavily
Who doesn't love a one line solution? Thanks!!Sarchet
Valid code: <meta name=\"viewport\" content=\"initial-scale=1.0\" />Burbot
Exactly! It is only prefixing your HTML code with it: stringHTML = "<meta name=\"viewport\" content=\"initial-scale=1.0\" />" + stringHTMLNne
what can go wrong -> if one time the font is OK, and when user tries to load the same html template but with another <body> contents, the font size is changed? <meta name='viewport' content='initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, width=400'>Emalia
Fantastic! I've been beating my head against the wall on this one. P.S. Do you have a solution for opening links from WKWebView to Safari??Berserker
Off the top of my head, you should be able to interrogate the URL being requested in WKNavigationDelegate, cancel the request, and reopen it in Safari via openURL in the AppDelegate.Elfin
I'm new to web development and Html and I also have this problem... where do I add this to? i have a very large Html that starts like this - "<?xml version=\"1.0\"?> <!DOCTYPE html> <html class=\"frank_ruehl mediaOverlayStyle0 blackMode fourtytwo\"xmlns=\"example.com/1999/xhtml\" xmlns:epub=\"example.come/2007/ops\"> <head>........"Padding
html = #"<meta name="viewport" content="initial-scale=1.0" />"# + htmlHassan
I'm loading WKWebview with URL. Where should I add this line with code.Potion

© 2022 - 2024 — McMap. All rights reserved.