我遇到了Android的這個問題,所以我創建了一個服務方法,我可以將它放入單個組件中。它基於在<ion-content>
標記內使用<ion-input>
字段。
這利用了Content
類中添加的setScrollTop
方法。
服務
export class KeyboardService {
autoKeyboardScroll(content:Content, scrollBackAfterKeyboardClose?:boolean) {
if (!content) {
return;
}
var previousScrollTop = null;
function onKeyboardShow(e) {
// if the content no longer exists, stop the listener
if (removeListenersForMissingContent()) {
return;
}
previousScrollTop = content.getScrollTop();
// find the input that's currently in focus
var focusedElement = document.activeElement;
if (focusedElement && ['INPUT', 'TEXTAREA'].indexOf(focusedElement.tagName)!==-1) {
// determine the total offset between the top of the "ion-content" and this element.
// we will do this by climbing up the dom until we reach the "ion-content"
var offsetTop = focusedElement.offsetTop + focusedElement.scrollHeight;
var parentEl = focusedElement.offsetParent;
while (parentEl && parentEl.tagName!=='ION-CONTENT') {
offsetTop += parentEl.offsetTop;
parentEl = parentEl.offsetParent;
}
// we want to move the input so that the bottom of the focused input is just above the keyboard
var contentDimensions = content.getContentDimensions();
var newScrollTop = offsetTop - (contentDimensions.contentHeight - focusedElement.scrollHeight);
content.setScrollTop(newScrollTop);
}
}
function onKeyboardHide(e) {
// if the content no longer exists, stop the listener
if (removeListenersForMissingContent()) {
return;
}
// set the scroll top back to the initial position, if requested
if (scrollBackAfterKeyboardClose) {
content.setScrollTop(previousScrollTop);
}
}
function removeListenersForMissingContent() {
// if there is no content, remove the keyboard listeners
if (!content || content.getContentDimensions().contentHeight===0) {
window.removeEventListener('native.keyboardshow', onKeyboardShow);
window.removeEventListener('native.keyboardhide', onKeyboardHide);
return true;
}
}
// setup listeners
window.addEventListener('native.keyboardshow', onKeyboardShow);
window.addEventListener('native.keyboardhide', onKeyboardHide);
}
}
組件
@Component({
template: `<ion-content>
<ion-list>
<ion-item>
<div style="height: 400px"></div>
</ion-item>
<ion-item>
<ion-label>Field 1</ion-label>
<ion-input type="text"></ion-input>
</ion-item>
<ion-item>
<ion-label>Field 2</ion-label>
<ion-input type="text"></ion-input>
</ion-item>
<ion-item>
<ion-label>Field 3</ion-label>
<ion-input type="text"></ion-input>
</ion-item>
<ion-item>
<ion-label>Field 4</ion-label>
<ion-input type="text"></ion-input>
</ion-item>
<ion-item>
<ion-label>Field 5</ion-label>
<ion-input type="text"></ion-input>
</ion-item>
<ion-item>
<ion-label>Field 6</ion-label>
<ion-input type="text"></ion-input>
</ion-item>
</ion-list>
</ion-content>`
})
export class MyPage {
@ViewChild(Content) content: Content;
constructor(private: keyboardService: KeyboardService) {}
// add the keyboard scroll action to this page. this is added after the view has been created,
// so the content element will be avaialble.
ionViewDidEnter() {
// timeout seems to be required, to ensure that the content child is available
setTimeout(() => {
// set the keyboard to auto-scroll to the focused input, when it opens
this.keyboardService.autoKeyboardScroll(this.content);
});
}
}
這對我來說就像一個魅力!謝謝 – user3153278
這一直讓我瘋狂。非常簡單的解決方案。 – Matt
答案像一個魅力工作,一直在努力與插件等..偉大的工作! – JoeriShoeby