Xamarin表单-iOS上的键盘覆盖输入(文本字段)



在Xamarin Forms页面(下面的代码(上,当用户在Android上点击Entry(文本字段(时,屏幕向上移动,键盘出现,这很好。然而,在iOS上,键盘覆盖了条目。在本机开发中,它需要在代码中处理,但如何在Xamarin Forms中解决这个问题?我认为唯一的解决方案是尝试常规的Xamarin并在那里开发单独的平台代码。

ScrollView中包装Entry元素

为此,您可以在构造函数中使用以下代码:

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);
}
}
}

正如@Jason所提到的,不要忘记将根布局添加到ScrollView中。

在我的例子中,我使用网格和堆栈,将根布局包装在ScrollView中,并将方向设置为两者都不。它在没有任何Nuget和代码的情况下工作。事实上,我尝试过IQKeyboardManager,但都不起作用,依赖性降低。感谢@Jason的创意。

参考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);
}

最新更新