当用户在 Xamarin Forms 页面上点击输入框(文本框)时,屏幕会向上移动并出现键盘,这很好。然而,在 iOS 上,键盘会遮挡输入框。在原生开发中,需要在代码中处理,但在 Xamarin Forms 中如何解决这个问题呢?我认为唯一的解决方案是尝试使用普通的 Xamarin 并在那里开发单独的平台代码。
当用户在 Xamarin Forms 页面上点击输入框(文本框)时,屏幕会向上移动并出现键盘,这很好。然而,在 iOS 上,键盘会遮挡输入框。在原生开发中,需要在代码中处理,但在 Xamarin Forms 中如何解决这个问题呢?我认为唯一的解决方案是尝试使用普通的 Xamarin 并在那里开发单独的平台代码。
Entry
元素包装在ScrollView
中。 public YourPage()
{
InitializeComponent();
this.yourEntry.Focused += (s, e) => { SetLayoutPosition(onFocus: true); };
this.yourEntry.Unfocused += (s, e) => { SetLayoutPosition(onFocus: false); };
}
实现以下方法:
void SetLayoutPosition(bool onFocus)
{
if (onFocus)
{
if (Device.RuntimePlatform == Device.iOS)
{
this.CenterStackLayout.TranslateTo(0, -100, 50);
}
}
else
{
if (Device.RuntimePlatform == Device.iOS)
{
this.CenterStackLayout.TranslateTo(0, 0, 50);
}
}
}
在我的情况下,我使用了网格和堆栈,将我的根布局包装在ScrollView中,并将方向设置为不确定。这样做可以不需要任何Nuget或代码后台就能实现效果。 实际上,我尝试过 IQKeyboardManager,但由于依赖问题已经废弃无法使用。 感谢 @Jason 提供的思路。
Orientation="Neither"
对我很有帮助。如果没有这个设置,ScrollView的内容大小会出现问题。 - BlueRaja - Danny Pflughoeft参考 https://forums.xamarin.com/discussion/151812/ios-keyboard-overlapping-entry
下面的库解决了我的问题。 https://github.com/TheEightBot/Xamarin.IQKeyboardManager
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
IQKeyboardManager.SharedManager.Enable = true;
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}