How do I make Xamarin.Forms.iOS View scroll to the Entry that is in focus?

xamarin forms scrollview scroll to bottom
xamarin forms scrollview events
xamarin ios scrollview not scrolling
xamarin auto scroll
xamarin scrolling text
xamarin listview scroll to item
xamarin forms listview auto scroll
xamarin scrollview not scrolling

I have a login page with 3 Entry controls under a large image logo and a Button control at the bottom of the page, when I click on the top Entry control to bring the Entry into focus to start typing, the keyboard appears and covers up the Entry controls below and the Login button which is not the best user experience.

The user can only scroll up manually on Xamarin.Forms.iOS but using the same code, ScrollToAsync, on Android I have been able to make the Entry on focus scroll to the top of page by grabbing the parent ScrollView and doing "ScrollView.ScrollToAsync(EntryControl, ScrollToPosition.Start, true);"

On Android, the button at the bottom of the page also moves up by using a * in the Grid Row definition of the empty space in the middle.

It seems like Xamarin.Forms.iOS behaves completely different to Xamarin.Forms.Android when rendering. I have seen the ScrollToAsync has some issues on iOS on page load due to the ScrollView not existing until the page has fully loaded. But this action is on a page that has already fully rendered. What am I doing wrong?

I have tried a Delay as mentioned in this SO, but it doesn't help. ScrollToAsync is not working in Xamarin.Forms

XAML

<Grid>
    <ScrollView VerticalOptions="FillAndExpand"
                BackgroundColor="White"
                x:Name="ScrollViewContainer"
                >
        <Grid Margin="15, 0, 15, 10">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" /><!--Logo-->
                <RowDefinition Height="Auto" /><!--Entry1-->
                <RowDefinition Height="Auto" /><!--Entry2-->
                <RowDefinition Height="Auto" /><!--Entry3-->                    
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" /><!--Button-->
                <RowDefinition Height="20" /><!--Empty padding-->
            </Grid.RowDefinitions>
            <Image Grid.Row="0"                
                   Aspect="AspectFit"
                   Source="CompanyLogo"
                   HorizontalOptions="Start"
                   HeightRequest="300"
                   VerticalOptions="Start"
                   Margin="10, 20, 20, 0"
                   />
             <Entry Grid.Row="1"
                    x:Name="BranchName"
                    Placeholder="BranchName" 
                    Focused="BranchName_Focused"
                    />   
             <Entry Grid.Row="2"
                    x:Name="Username"
                    Placeholder="e.g. Batman "
                    Focused="Username_Focused"
                    />  
             <Entry Grid.Row="3"
                    x:Name="Password"
                    Placeholder="Enter your password"
                    Focused="Password_Focused"
                    IsPassword="True"
                    />              
             <Button Grid.Row="5"
                     x:Name="LoginButton"
                     Text="Log in"
                     />
        </Grid>
    </ScrollView>
</Grid>

Code Behind

    private async void BranchName_Focused(object sender, FocusEventArgs e)
    {
        await ScrollViewContainer.ScrollToAsync(BranchName, ScrollToPosition.Start, true);
    }

The solution was in part by @numan98 answer above which got me re-taking another look at the microsoft docs, https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.scrollview.scrolltoasync?view=xamarin-forms

It states that ScrollToAsync(double X, double Y, bool animate) will be the final double position values of where X and Y should be.

Thus, X doesn't move anywhere, so I believe it should be 0, and Y I just gave an arbitrary number, such as 250. Somewhere close to the top I believe.

After, we have that iOS bug again where you need to put in a delay.

The final code looks something like this.

private async void BranchName_Focused(object sender, FocusEventArgs e)
{
    Device.StartTimer(TimeSpan.FromSeconds(0.25), () =>
    {
        scroll.ScrollToAsync(0, 250, true);
        return false;
    );
}

I hope this helps someone. The next thing now is to get the CTA button to move and always be in view.

ScrollView.ScrollToAsync Method (Xamarin.Forms), Returns a task that scrolls the scroll view to a position asynchronously. If you want to shrink the iOS view, the same as Android, you can do this via a custom renderer. Have a look at the SoftInput KeyboardRender. UWP. UWP refers to the Soft Keyboard as an Input Pane. By default UWP does an AdjustPan, the same as Android, with no scroll capability. In order to do a resize, I put together this code in your MainPage.cs

Add this in your xaml.cs file

 protected override void OnAppearing()
    {
        base.OnAppearing();
        userName.Focused += InputFocused;
        password.Focused += InputFocused;
        userName.Unfocused += InputUnfocused;
        password.Unfocused += InputUnfocused;
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        userName.Focused -= InputFocused;
        password.Focused -= InputFocused;
        userName.Unfocused -= InputUnfocused;
        password.Unfocused -= InputUnfocused;
    }
    void InputFocused(object sender, EventArgs args)
    {
        Content.LayoutTo(new Rectangle(0, -270, Content.Bounds.Width, Content.Bounds.Height));
    }

    void InputUnfocused(object sender, EventArgs args)
    {
        Content.LayoutTo(new Rectangle(0, 0, Content.Bounds.Width, Content.Bounds.Height));
    }

How to Scroll the screen upto last entry in Xamarin Forms , I am doing an app in which i have to submit data having 8 entries one by one in a stack. the handler explicitly moving the focus to the next Entry and explicitly scrolling your ScrollView. What View have you hooked OnLabelFocsed to? The problem is only with android and in iOS it is working good. Returns a task that scrolls the scroll view to a position asynchronously. ScrollToAsync(Element, ScrollToPosition, Boolean) Returns a task that scrolls the scroll view to an element asynchronously. SendScrollFinished() For internal use by the Xamarin.Forms platform. SetBinding(BindableProperty, BindingBase) Assigns a binding to a property.

I think you should try this...

private async void BranchName_Focused(object sender, FocusEventArgs e)
{
   await ScrollViewContainer.ScrollToAsync(BranchName.Bounds.X, BranchName.Bounds.Y, true);
}

Creating Mobile Apps with Xamarin.Forms Preview Edition 2, The root element of the XAML page sets the Title property, but that's ignored on When the Editor has input focus on Windows Phone, it displays a light an Entry view is positioned toward the bottom of the screen, you want to put it in However, because Editor implements its own scrolling, you can't put it in a ScrollView. Make sure that the background and text colors you choose are usable on each platform and don't obscure any placeholder text. Interactivity. Editor exposes two events: TextChanged – raised when the text changes in the editor. Provides the text before and after the change.

ScrollView, To add multiple views within the scroll view, make the direct child you add a view For vertical scrolling, consider NestedScrollView instead of scroll view which  You can see on the 2nd screenshot that the keyboard slides up covering the entry view. While on the 3rd screenshot, the Nuget package fixes the issue by automatically moving up the entry view along with the keyboard. To sum up, Android does not have keyboard-sliding up issue out of the box, whereas iOS has it by default.

Forms Bugs, 21373 – [Android only] An Entry in a ViewCell loses focus immediately 22389 – Android - ListView Item doesn't get removed when an item deleted from ItemSource 23084 – ScrollView does not enable scrolling correct in iOS (​iPhone5) Entry is a single line text entry. It is best used for collecting small discrete pieces of information, like usernames and passwords. The following example creates a new username and password set of entries.

Ios scroll focus, Follow these steps to enable Firefox Focus for Safari: Step one: allow Safari to use content scrolling the page to the input field that get's in focus?Xamarin. Annoying iOS Safari input Step 3, Determine the direction. forms ios application. The view should look and behave mostly the same on both Android and iOS. The goal for us was to learn if the new Visual feature was easy to use, how helpful the Material Design controls were for meeting design needs, and what we should do next to make this capability really sing.

Comments
  • This code not compile. Missing }); in end.
  • Hi, thank you for your answer, I had just tried this and it did not work. Maybe I am missing something. From the Microsoft docs docs.microsoft.com/en-us/dotnet/api/… and this xfcomplete.net/xamarin.forms/2016/01/14/… can you explain how LayoutTo can help with the solution to the problem? Thanks
  • Hi, You can refer this link stackoverflow.com/questions/39641429/…
  • Didn't help but thank you. I actually require the exact same effect and the plugin in your SO link didn't work for me neither. I found another solution
  • @CookieMonster Okey Happy coding bro. +1 from me
  • Yes, reading MS docs I realize that there are 2x ways to overload this method. docs.microsoft.com/en-us/dotnet/api/…