import { Directive, ElementRef, Input, OnDestroy, OnInit } from '@angular/core'; | |
import { Keyboard } from '@ionic-native/keyboard'; | |
import { Content, Platform } from 'ionic-angular'; | |
import { Subscription } from 'rxjs/Subscription'; | |
/** | |
* @name KeyboardAttachDirective | |
* @source https://gist.github.com/Manduro/bc121fd39f21558df2a952b39e907754 | |
* @description | |
* The `keyboardAttach` directive will cause an element to float above the | |
* keyboard when the keyboard shows. Currently only supports the `ion-footer` element. | |
* | |
* ### Notes | |
* - This directive requires [Ionic Native](https://github.com/driftyco/ionic-native) | |
* and the [Ionic Keyboard Plugin](https://github.com/driftyco/ionic-plugin-keyboard). | |
* - Currently only tested to work on iOS. | |
* - If there is an input in your footer, you will need to set | |
* `Keyboard.disableScroll(true)`. | |
* | |
* @usage | |
* | |
* ```html | |
* <ion-content #content> | |
* </ion-content> | |
* | |
* <ion-footer [keyboardAttach]="content"> | |
* <ion-toolbar> | |
* <ion-item> | |
* <ion-input></ion-input> | |
* </ion-item> | |
* </ion-toolbar> | |
* </ion-footer> | |
* ``` | |
*/ | |
@Directive({ | |
selector: '[keyboardAttach]' | |
}) | |
export class KeyboardAttachDirective implements OnInit, OnDestroy { | |
@Input('keyboardAttach') content: Content; | |
private onShowSubscription: Subscription; | |
private onHideSubscription: Subscription; | |
constructor( | |
private elementRef: ElementRef, | |
private keyboard: Keyboard, | |
private platform: Platform | |
) {} | |
ngOnInit() { | |
if (this.platform.is('cordova') && this.platform.is('ios')) { | |
this.onShowSubscription = this.keyboard.onKeyboardShow().subscribe(e => this.onShow(e)); | |
this.onHideSubscription = this.keyboard.onKeyboardHide().subscribe(() => this.onHide()); | |
} | |
} | |
ngOnDestroy() { | |
if (this.onShowSubscription) { | |
this.onShowSubscription.unsubscribe(); | |
} | |
if (this.onHideSubscription) { | |
this.onHideSubscription.unsubscribe(); | |
} | |
} | |
private onShow(e) { | |
let keyboardHeight: number = e.keyboardHeight || (e.detail && e.detail.keyboardHeight); | |
this.setElementPosition(keyboardHeight); | |
}; | |
private onHide() { | |
this.setElementPosition(0); | |
}; | |
private setElementPosition(pixels: number) { | |
this.elementRef.nativeElement.style.paddingBottom = pixels + 'px'; | |
this.content.resize(); | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
We are exploring ionic 2. Can you please let us know how to use this directive and where? |
This comment has been minimized.
This comment has been minimized.
Hi man, really nice directive. But can we somehow achieve a smoother movement of the footer. E.g. a transition or something? Many thanks, Diesi |
This comment has been minimized.
This comment has been minimized.
Hi! Thanks for this! Any help? |
This comment has been minimized.
This comment has been minimized.
For a better transition you should use the WkWebView Plugin...@diesenreither |
This comment has been minimized.
This comment has been minimized.
@pinrostudio You can find an example in the comment block. @ddiesenreither You could certainly add an animation, but it's impossible to recreate the animation of the native iOS keyboard. So it will never be nicely pushed up by the keyboard. @arturalkaim This directive does not do anything on Android, only on iOS. It should work if you follow the example. |
This comment has been minimized.
This comment has been minimized.
good job! |
This comment has been minimized.
This comment has been minimized.
@Manduro Thanks for the directive! |
This comment has been minimized.
This comment has been minimized.
@schubertq have you resolve your issue? unfortunately have the same ;/ |
This comment has been minimized.
This comment has been minimized.
@schubertq @matejamateusz |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
My Fork solves this error. I couldn't test it on a device yet. |
This comment has been minimized.
This comment has been minimized.
Great snippet.. but having the same issue with the send button.. keyboard pushed the footer up perfectly but clicking the send button makes the keyboard retreat and the send button drop down also, it is then necessary to click it a second time when it is back in its resting state.. anyone have any fixes for this yet ? |
This comment has been minimized.
This comment has been minimized.
Same issue with the send button, any update? |
This comment has been minimized.
This comment has been minimized.
To fix the issue with the send button, you can add the following code in your css file: .input-cover { |
This comment has been minimized.
This comment has been minimized.
@mysecondworld I ran into this as well some time ago and I believe the problem is tied to a specific version of @schubertq @pdrosos @matejamateusz @REPTILEHAUS @derasmus @antoine92190 <ion-footer [keyboardAttach]="content">
<ion-toolbar>
<input type="text" #input>
<button ion-button (tap)="send(input, $event)">Send</button>
</ion-toolbar>
</ion-footer> send(input: HTMLInputElement, event: MouseEvent) {
// Prevent default behaviour of this click event
event.preventDefault();
// Insert logic for actually sending data here
// Reset input to empty and set focus
input.value = '';
input.focus();
} |
This comment has been minimized.
This comment has been minimized.
I am still struggling with the ios keyboard problem. The usage as described above didn't work for me, I have to use it with |
This comment has been minimized.
This comment has been minimized.
@alain-sarti Make sure to include the directive in your app.module.ts and use the [] around it in your HTML. Try the guide in the Angular docs if you need more info! |
This comment has been minimized.
This comment has been minimized.
@Manduro Please help me to solve this issue.I integrate everything as mentioned above in app.module.ts also initialise the directory.I don't know why this error is coming ? |
This comment has been minimized.
This comment has been minimized.
@Manduro I found the solution i missed in the app.modules after import i didnt mention the @ngModules ({declaration:[KeyboardAttachDirective]}) for this only it show this errors if anyone have related to this problem please follow this .The directory is working fine thanks man. |
This comment has been minimized.
This comment has been minimized.
@antoine92190 I have the click event on tag and the click event still won't fire with input-cover css. @Manduro, I'm not trying to keep the focus in the input box. But instead trying to click on a button and the button will trigger another function. However, since there's another listener behind the footer, it consistently trigger that event instead of whatever is on the keyboardAttached button. I've tried event.stopPropagation() and it stills brings that up and doesn't handle the click event on the button in the footer. |
This comment has been minimized.
This comment has been minimized.
It turns out that the event on footer never gets fired because setElementPosition happened too early. Not sure what's a good approach this. setTimeout does the trick but not reliably and cause some strange lag on when to resize/reposition the footer content. If I switched it to a faster listener like tap, it will work almost all the time having it fire quick enough. |
This comment has been minimized.
This comment has been minimized.
"tap event" works for ionic 3.0.1! confirmed! Thanks a lot! |
This comment has been minimized.
This comment has been minimized.
@jerrydeng tap event works fine, thanks for the tip |
This comment has been minimized.
This comment has been minimized.
Is there anyway of getting the footer to slowly come up, i.e. add a transition rather than instantly moving position to accomodate the keyboard? |
This comment has been minimized.
This comment has been minimized.
@Manduro? how can I use this directive? I want to know the step by step tutorial |
This comment has been minimized.
This comment has been minimized.
hi , I have a problem with the returned |
This comment has been minimized.
This comment has been minimized.
Hello guys i use above code and i call in my html page as below <ion-content padding #content> <ion-footer no-padding no-margin class="custom-footer" [keyboardAttach]="content"> |
This comment has been minimized.
This comment has been minimized.
@harshal111 Add keyboard provider in app.module.ts:
|
This comment has been minimized.
This comment has been minimized.
I suggest controll scroll with onShow, when keyboard open.
thanks. |
This comment has been minimized.
This comment has been minimized.
it‘work,but the scollcontent is to large |
This comment has been minimized.
This comment has been minimized.
+1 Worked for me. |
This comment has been minimized.
This comment has been minimized.
I wrote keyboard attach directive for iOS 11. please reference. |
This comment has been minimized.
This comment has been minimized.
Well done dude. Tested this on Android on Ionic 3 and it works as well. You just need to remove the platform check for iOS if using it on Android. |
This comment has been minimized.
This comment has been minimized.
Works great on iOS 11.2 |
This comment has been minimized.
This comment has been minimized.
This works on android as well, if you just add the following to the directive. Why is it not already in there? ngOnInit() {
if (this.platform.is('cordova') && this.platform.is('ios') || this.platform.is('android')) {
this.onShowSubscription = this.keyboard.onKeyboardShow().subscribe(e => this.onShow(e));
this.onHideSubscription = this.keyboard.onKeyboardHide().subscribe(() => this.onHide());
}
} Notices the |
This comment has been minimized.
This comment has been minimized.
I've added the directive to app.module:
But I keep getting this error. Anyone who has a hint?
My HTML:
|
This comment has been minimized.
This comment has been minimized.
I think this is not working with iPhone X or? |
This comment has been minimized.
This comment has been minimized.
When I emulate on iPhone X theres space between the footer and the keyboard. As if the keyboardheight is to high? Any fix for that? |
This comment has been minimized.
This comment has been minimized.
@ejerskov @NgModule({ declarations: [ MyApp, GroceryListMorePage, KeyboardAttachDirective ], But still getting this error => Can't bind to 'keyboardAttach' since it isn't a known property of 'ion-footer'.
|
This comment has been minimized.
This comment has been minimized.
Thanks @Manduro This worked perfectly with ionic 3, ios and android. |
This comment has been minimized.
This comment has been minimized.
Hey @abdelsalamally try importing the
|
This comment has been minimized.
This comment has been minimized.
Hello @prescindivel I'm still getting the same error even after adding "Providers: [Keyboard] in app.component , I am afraid maybe I missed something by setting up the directive , would you mind elaborating step by step how you made this work ... Thanks in advance I've been struggling with this stuff for days now |
This comment has been minimized.
This comment has been minimized.
@abdelsalamally what version of the ionic are you using? |
This comment has been minimized.
This comment has been minimized.
This is my ionic info : ` Ionic CLI : 5.0.0 (C:\Users\Abd El Salam\AppData\Roaming\npm\node_modules\ionic) Cordova: Cordova CLI : 9.0.0 Utility: cordova-res : not installed System: Android SDK Tools : 26.1.1 (C:\Users\Abd El Salam\AppData\Local\Android\Sdk) I first created a file called keyboard-attach.directive.ts in a folder called models then I imported it like this in my app.module.ts
then inside @ngModules like this: ],` Then in my html file where I want the keyboardattach to take effect like this:
But in the .ts of the above html I didn't do anything so when I run ionic cordova build android --prod I get this following nightmare : `ionic-app-scripts.cmd build --prod --target cordova --platform android [12:04:10] ionic-app-script task: "build" I even tried your solution of putting providers:[Keyboard] in app.component.ts but still the same issue , I also tried to include @ngModules.schemas like this to avoid the error: ` ], But still no way!!!!!` It's been days and days trying to find out how this ion-footer would go above the keyboard when someone tries to write something, actually the problemI have is that whenever I press in the "writing message" input , the keyboard covers the input and I can't see what I'm writing until I cancel the keyboard.. I thought perhaps this directive would tackle the issue but I can't manage to get ride of the above error. Please @prescindivel any help would be appreciated .Thanks |
This comment has been minimized.
This comment has been minimized.
@abdelsalamally I'm use lazy load. |
This comment has been minimized.
This comment has been minimized.
Thanks a lot @prescindivel let me try out |
This comment has been minimized.
This comment has been minimized.
Updated version for ionic 4 and cordova-plugin-ionic-keyboard (https://github.com/driftyco/ionic-plugin-keyboard is deprecated) Don't forget to install https://github.com/ionic-team/cordova-plugin-ionic-keyboard and include the directive on your page.module.ts
|
This comment has been minimized.
This comment has been minimized.
What about ionic 5? This is not working and capacitor |
This comment has been minimized.
This comment has been minimized.
any example for ionic 4 ? |
This comment has been minimized.
This comment has been minimized.
Hello, were you able to figure out it for ionic 4? I have some troubles. |
This comment has been minimized.
This comment has been minimized.
Hello, any working solution on capacitor (Ionic 5)? |
This comment has been minimized.
This comment has been minimized.
@alfredcarro @josedejesusAmaya @ipehimanshu @Jonatthu |
This comment has been minimized.
This comment has been minimized.
My Fix on this was adding a hide and show when the user clicks on the input. |
This comment has been minimized.
well done guy!