Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent keyboard push up webview at iOS app using phonegap

Tags:

When an input field at bottom of the screen has focus, the keyboard pushes up my webview and the upper part of the page is not visible anymore.

I want to prevent the keyboard pushing up the webview.

Anyone has an idea ?

like image 869
Yajap Avatar asked Dec 11 '12 12:12

Yajap


2 Answers

On focus, set window.scrollTo(0,0); This prevents keyboard from pushing up webview completely

$('input').on('focus', function(e) {
    e.preventDefault(); e.stopPropagation();
    window.scrollTo(0,0); //the second 0 marks the Y scroll pos. Setting this to i.e. 100 will push the screen up by 100px. 
});

If you don't want to set a static value for your Y scroll position, feel free to use this short plugin I've written that automatically aligns the keyboard underneath the input field. It's not perfect, but it does the job. Just call this on focus as shown above:

setKeyboardPos(e.target.id);
like image 168
Jonathan Avatar answered Sep 20 '22 15:09

Jonathan


Solved this issue using driftyco/ionic-plugins-keyboard (https://github.com/driftyco/ionic-plugins-keyboard)

First install keyboard plugin:

cordova plugin add com.ionic.keyboard

Now you can I) either disable native keyboard scrolling:

cordova.plugins.Keyboard.disableScroll(true);

or II) listen on native.keyboardshow event in deviceready and scroll back to top when keyboard shows:

window.addEventListener('native.keyboardshow', keyboardShowHandler);

function keyboardShowHandler(e){
    setTimeout(function() {
        $('html, body').animate({ scrollTop: 0 }, 1000);
    }, 0);
}

I used the II) approach because I liked the animated scrolling in my case. If you don't want to go with the animation replace the corresponding line with window.scrollTo(0, 0);. But I'm afraid that in that case you will again have to deal with this "litter jitter animation" Imskull wrote about.

like image 45
capitannaranja Avatar answered Sep 19 '22 15:09

capitannaranja