Ionic 4 键盘覆盖输入字段

ami*_*ros 6 cordova ionic-framework ionic4

我有一个 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>
Run Code Online (Sandbox Code Playgroud)

Mar*_*lin 11

我目前正在将 Ionic4 与 Cordova 9 和所有最新软件包一起使用,但在框架中找不到任何适合我的设置。最后我做了这个完全绕过框架的解决方法。它有一些动画,看起来还不错,所以我一直在使用它,直到框架正确解决了这个问题。

global.scss

ion-app {
    /*animation of native keyboard show*/
    transition: margin 300ms;
}
Run Code Online (Sandbox Code Playgroud)

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";
    });
}
Run Code Online (Sandbox Code Playgroud)


lok*_*usu 0

我已经暂时解决了这个 Ionic 错误:

...
<ion-texarea (ionFocus)="fixTextareaBug()">
...
Run Code Online (Sandbox Code Playgroud)

并在你的.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);
}
Run Code Online (Sandbox Code Playgroud)

我希望它能解决你的问题