我正在用Nativescript创建一个聊天UI界面,几乎所有的东西都能用,但在iOS中遇到了一些问题,我无法解决。在安卓系统中一切都能正常工作。
问题1:
问题2.当我在TextView中开始打字时,它立即转移到屏幕底部,隐藏在键盘后面,我看不到我在打什么。
这里是我在Playground上创建的一个演示项目,显示了这个问题。Playground演示项目
下面是一个GIF,显示这两个问题。
希望得到任何帮助。谢谢!我正在用Nativescript创建一个聊天界面,几乎所有的东西都能用,但在iOS中遇到了一些问题,我无法解决。
问题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);
}
};