7

When a user logs in to my app from an iPhone/iPad, Safari (helpfully) zooms in while the user is filling out the username/password fields. But when the form is submitted and we log them in, we don't reload the page (this is a single page application), so the zoom is never reset. So the app is always started at a zoomed in scale.

I have looked at Jeremy Keith's solution, which successfully resets the zoom, but also prevents future scaling/zooming by the user, because he sets the maximum-scale of the viewport.

Like this:

var viewportmeta = document.querySelector('meta[name="viewport"]');

if (viewportmeta) {
    viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0';
}

Has anyone seen a nice solution for reseting this after a form submission, without freezing up the viewport afterwards?

Bryce Johnson
  • 6,689
  • 6
  • 40
  • 51

1 Answers1

7

What I found to work is a little hacky, but seems effective. Basically, on form submission, I'm setting the maximum-scale, and then immediately removing it. Hope this helps someone.

element.on('submit', function(event) {
    if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
      var viewportmeta = document.querySelector('meta[name="viewport"]');
      if (viewportmeta) {
        viewportmeta.setAttribute('content', 'width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0');
        viewportmeta.setAttribute('content', 'width=device-width, minimum-scale=1.0, initial-scale=1.0');
      }
   }
}
Bryce Johnson
  • 6,689
  • 6
  • 40
  • 51
  • 1
    As of iOS 9, I have had to also add `shrink-to-fit=no` - [Source](http://www.wmyl.se/en/blog/safari-ios-9-causing-issues-responsive-sites/) – Connor Finn McKelvey Dec 14 '15 at 20:36
  • 1
    This seems to have stopped working in iOS 10. Would be interested in a fix. – Magnar Sep 18 '16 at 19:42
  • Thanks; this was very helpful. I'll offer an improvement of a few lines: `var viewportContentOrig = viewportmeta.getAttribute('content'); viewportmeta.setAttribute('content', 'width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0'); viewportmeta.setAttribute('content', viewportContentOrig);` – Ryan Jan 29 '17 at 22:56
  • 2
    I just found out that iOS requires a minimum of 16px font on form inputs (or else it auto-zooms in and then doesn't auto-zoom back out after the form), which was my other problem: http://stackoverflow.com/questions/37808180/disable-viewport-zooming-ios-10-safari#comment71065486_38573198 – Ryan Jan 30 '17 at 18:16