I've been trying to make this work for days now, with no luck, so I thought I'd share info and maybe someone comes up with a solution.
I want to get the exact viewport size when the soft keyboard is up. I am currently using the following code on the header in order for the site to be responsive:
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
What I realized is that when the soft keyboard comes up, it uses the device height as the viewport height and pushes the rest of the site upwards - which makes me assume that it's getting it's height from the width=device-width option.
Using the following code after initiating the soft keyboard:
setTimeout(function() {
viewport = document.querySelector("meta[name=viewport]");
viewport.setAttribute('content', 'height=auto');
}, 300)
And then getting the height using jquery shows CORRECT results on Android - aka the visible viewport size WITHOUT the virtual keyboard, but not on iOS. I am getting a random number, which I assume stems from the rest of the meta tag not existing, so I am getting a zoomed-in version of the website along with a number like 75 or 100 (on iphone 4s)
I also tried creating a fixed element after bringing the keyboard up, making it use the viewport height with top:0; and bottom:0; attributes, but I still get the original height.
What comes closer to this, is setting the viewport meta tag height to a fixed value, that can be picked up by jquery's $(window).height()
, which means that the meta tag actually makes a difference after initiating the keyboard, but there is no true value for a fixed height.
I've seen lots of topics around this, but none with a solution. Anyone that has a solution for this?
The viewport dimensions come from the size of the UIWebView
element.
As mentioned in the previous answer there are two ways to handle adapting to the on screen keyboard. You can resize the view or adjust the content insets.
The dimensions of the viewport on iOS are reporting the size of the view so resizing would adjust the viewport. Safari adjusts the insets and keeps the view the same size so the viewport does not change.
If you were to make assumptions that touch devices generally utilize on screen input rather than external keyboards you can programmatically adjust the size on your own by taking the device height or width depending on orientation and factor in a multiplier for portion of the screen consumed by the on screen keyboard.
I utilize a viewport class to act as a proxy to give me most likely real viewport dimensions rather than browser reported and binds to input elements to track state of input elements and orientation changes to dynamically modify the multiplier applied when requesting the viewport dimensions.