I want my message input bar to float above the keyboard when the keyboard shows but it looks like there's no keyboard-attach directive (like v1) in Ionic 2 yet (maybe in the works?). Is there any alternative/workaround?
Current behaviour:
Wanted behaviour:
Here's the code of my message input bar:
<ion-toolbar position="bottom" *ngIf="userIsAdmin">
<form (ngSubmit)="onSubmit(f)" #f="ngForm" class="message-form">
<ion-badge class="message-form-badge">Admin</ion-badge>
<ion-input type="text" placeholder="Type a message..." ngControl="messageInput"></ion-input>
<button type="submit" small class="message-form-button">Send <ion-icon name="send"></ion-icon></button>
</form>
</ion-toolbar>
I was having this problem with Android, so I created a service method that I could put into individual components. It's based on using
<ion-input>
fields inside of an<ion-content>
tag.This takes advantage of the
setScrollTop
method that was added to theContent
class.Service
Component
I found a solution which works for me on IOS.
When you inspect the
<ion-item>
with<ion-input>
in the browser(debugging use Safari for IOS) you can find that ionic generates a<div class='input-cover'>
which has the styleposition: absolute;
.Write a CSS which overrides this as below
This did the trick for me and now when you focus on an input field, it scrolls into view and does not hide below the keyboard anymore and all this works buttery smooth.
The solution I ended up using and one that I'm satisfied with is:
Keyboard.disableScroll(true);
<input type="text">
instead of<ion-input type="text">
Works perfectly now!
I've also needed to implement this. I did it and it works perfectly.
1st you need to use cordova plugins keyboard, and at the start call
cordova.plugins.Keyboard.disableScroll(true);
so the keyboard will not push your view up. 2nd you need to listen on keyboardshow and keyboard hide events like this with a the handlers:Than you can make an observable from event like this:
Than you can listen to that observable. If the keyboard is open than you change the height of the container where your messages are shown. You basically have to make it lower for the height of the keyboard. Here is how I did that
and you change these properties with ngStyle like this
I also needed this for chatapp, and now it works perfectly (even if you rotate the screen portrait/landscape mode), the input always floats above the keyboard just like in the native apps :)
I hope this will help you!