我有一个Ionic 4应用,其中包含一个带有输入的表单。
当用户单击输入时,它会打开键盘,但会隐藏内容,而不进行滚动。
有什么办法解决吗?
这是我的代码:
<form #f="ngForm" (ngSubmit)="sendMail()">
<ion-item>
<ion-label position="floating">name
</ion-label>
<ion-input [(ngModel)]="senderName">
</ion-input>
</ion-item>
<ion-item>
<ion-label position="floating">mail
</ion-label>
<ion-input [(ngModel)]="senderMail">
</ion-input>
</ion-item>
<ion-item class="borderedTextArea">
<ion-textarea [(ngModel)]="mailText" style="height:150px;"></ion-textarea>
</ion-item>
<ion-button type="submit" style="float:left">send</ion-button>
</form>
答案 0 :(得分:0)
我已通过以下方式临时解决了该离子错误:
...
<ion-texarea (ionFocus)="fixTextareaBug()">
...
和您的.ts
@ViewChild(IonTextarea)
public ionTextArea: IonTextarea;
private focusFix = false;
...
...
public fixTextareaBug() {
setTimeout(() => {
if (this.focusFix) {
this.focusFix = false;
return;
}
(this.ionTextArea as any).el.lastElementChild.blur();
this.focusFix = true;
(this.ionTextArea as any).el.lastElementChild.focus();
}, TEXTAREA_TIMEOUT);
}
我希望它能解决您的问题
答案 1 :(得分:0)
我也遇到了这个问题,但仅在android中,我所做的是创建一个脚本来获取焦点元素和键盘的高度,并使用jQuery添加了marginTop来在键盘上方将主体移到键盘上方键盘显示,这是我的代码:
constructor(
private platform: Platform,
private keyboard: Keyboard
) {
if(this.platform.is('android')){
this.keyboard.onKeyboardShow().subscribe((e) => {
const offset = $(document.activeElement).offset().top;
let height = (offset - e.keyboardHeight)*-1;
height = height > 0 ? 0 : height;
$('body').animate({ 'marginTop': height + 'px' }, 100);
});
this.keyboard.onKeyboardHide().subscribe(e => {
$('body').animate({ 'marginTop': 0 + 'px' }, 100);
});
}
}
需要的库:
npm install jquery
npm install @types/jquery
ionic cordova plugin add cordova-plugin-ionic-keyboard
npm install @ionic-native/keyboard
进口
import { Platform } from '@ionic/angular';
import * as $ from "jquery";
import { Keyboard } from '@ionic-native/keyboard/ngx';
这不是一个优雅的解决方案,但它可以工作
答案 2 :(得分:0)
我目前正在将Ionic4与Cordova 9和所有最新软件包一起使用,但是在适合我的框架中找不到任何设置。最后,我提出了一种完全绕开框架的解决方法。它有一点动画,看起来还不错,所以我一直在使用它,直到框架正确解决为止。
global.scss
ion-app {
/*animation of native keyboard show*/
transition: margin 300ms;
}
app.component.ts
declare var $: any;
ngAfterViewInit() {
// This element never changes.
let ionapp = document.getElementsByTagName("ion-app")[0];
window.addEventListener('keyboardDidShow', async (event) => {
// Move ion-app up, to give room for keyboard
let kbHeight: number = event["keyboardHeight"];
let viewportHeight: number = $(window).height();
let inputFieldOffsetFromBottomViewPort: number = viewportHeight - $(':focus')[0].getBoundingClientRect().bottom;
let inputScrollPixels = kbHeight - inputFieldOffsetFromBottomViewPort;
// Set margin to give space for native keyboard.
ionapp.style["margin-bottom"] = kbHeight.toString() + "px";
// But this diminishes ion-content and may hide the input field...
if (inputScrollPixels > 0) {
// ...so, get the ionScroll element from ion-content and scroll correspondingly
// The current ion-content element is always the last. If there are tabs or other hidden ion-content elements, they will go above.
let ionScroll = await $("ion-content").last()[0].getScrollElement();
setTimeout(() => {
$(ionScroll).animate({
scrollTop: ionScroll.scrollTop + inputScrollPixels
}, 300);
}, 300); // Matches scroll animation from css.
}
});
window.addEventListener('keyboardDidHide', () => {
// Move ion-app down again
// Scroll not necessary.
ionapp.style["margin-bottom"] = "0px";
});
}
答案 3 :(得分:0)
我通过降级键盘插件来解决这个问题
ionic cordova plugin remove cordova-plugin-ionic-keyboard
ionic cordova plugin add cordova-plugin-ionic-keyboard@2.0.5
然后删除android平台并再次添加
答案 4 :(得分:0)
使用ionFocus事件和scrollToBottom函数简单地解决它,然后在ionFocus中调用它,因此当您专注于输入时,内容将滚动到底部强文本