问题1:
这是因为 IQKeyboardManager 在 ViewController (Page) 之上托管了一个 ScrollView,而 RadListView 有自己的可滚动区域。解决方案是在键盘处于活动状态时调整插图。
下面的代码获取键盘高度,并在首次显示时将其缓存在应用程序对象中。
import * as application from "@nativescript/core/application";
let observer = application.ios.addNotificationObserver(
UIKeyboardDidShowNotification,
notification => {
application.ios.removeNotificationObserver(
observer,
UIKeyboardDidShowNotification
);
(application as any)._keyboardHeight = notification.userInfo.valueForKey(
UIKeyboardFrameBeginUserInfoKey
).CGRectValue.size.height;
}
);
当文本字段聚焦时,调整列表视图的插图
textFieldFocus() {
console.log("Focus on TextField");
(this
.radList as any)._originalContentInset = this.radList.ios.contentInset;
if ((application as any)._keyboardHeight) {
this.radList.ios.contentInset = UIEdgeInsetsMake(
(application as any)._keyboardHeight,
0,
0,
0
);
} else {
setTimeout(() => this.textFieldFocus(), 100);
}
}
问题2:
这是因为文本视图会在文本更新时自行布局。防止这种情况可能会保持滚动位置不变。下面的覆盖检查字段是否聚焦并忽略布局调用。
import { TextView } from "@nativescript/core/ui/text-view";
TextView.prototype.requestLayout = function() {
if (
!arguments[0] &&
this.nativeViewProtected &&
this.nativeViewProtected.isFirstResponder
) {
this.nativeViewProtected.setNeedsLayout();
IQKeyboardManager.sharedManager().reloadLayoutIfNeeded();
} else {
View.prototype.requestLayout.call(this);
}
};
更新游乐场