Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms - Keyboard covers Entry (text field) on iOS

On Xamarin Forms page (code below) when user taps on Entry (text field) on Android, the screen moves up and keyboard appears which is fine. However, on iOS keyboard covers the Entry. In native development it needs to be handled in code but how can this problem be resolved in Xamarin Forms? I think only solution is to try regular Xamarin and develop separate platform code there.

like image 760
KirillC Avatar asked Jul 15 '18 00:07

KirillC


4 Answers

Wrap your Entry elements in a ScrollView

like image 115
Jason Avatar answered Nov 14 '22 11:11

Jason


For that you can use below code in constructor:

    public YourPage()
    {
        InitializeComponent();
        this.yourEntry.Focused += (s, e) => { SetLayoutPosition(onFocus: true); };
        this.yourEntry.Unfocused += (s, e) => { SetLayoutPosition(onFocus: false); };            
    }

Implement method like:

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

Don't Forgot to add your Root Layout into ScrollView, as @Jason has mentioned.

like image 25
Srusti Thakkar Avatar answered Nov 14 '22 12:11

Srusti Thakkar


In my case, I'm using grid and stacks, wrap my root layout in a ScrollView, and set the orientation to neither. And it works without any Nuget and code-behind. Actually, I tried the IQKeyboardManager, but none of it works, dependency deprecation. Thanks to @Jason for the idea.

like image 3
Dean Thomas Avatar answered Nov 14 '22 12:11

Dean Thomas


Referring to https://forums.xamarin.com/discussion/151812/ios-keyboard-overlapping-entry

Below library solved my issue. 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);
}
like image 1
TPG Avatar answered Nov 14 '22 12:11

TPG