Search code examples
xamarin.formsstacklayout

Xamarin forms: Top stacklayout is not visible in UI when keyboard is on


My Xaml:

<StackLayout
   Orientation="Vertical">

   <StackLayout
      Orientation="Horizontal"> 
      //Group labels and back arrow
   </StackLayout>

   <ListView>
      //Message list
   </ListView>

   <Grid>
   //Plus symbol,Editor and send icon
   </Grid>

</StackLayout>

Screenshot:

enter image description here

Issue:

In normal screen there is a bar on the top(red circled). When click the editor on the bottom top bar is hiding. I need the top bar always on top. This issue is only in IOS part, in android this feature is working fine. How can I fix this issue?


Solution

  • Like the picture in the question my soft keyboard always touching the Editor. So I added a custom renderer for my Editor to solve that issue. After adding that custom renderer the top bar is always sticking on the top of the page.

    I used the following custom renderer to solve the keyboard overlapping issue:

    using System;
    using UIKit;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.iOS;
    using HBIClientFacingApp;
    using HBIClientFacingApp.iOS;
    
    [assembly:ExportRenderer( typeof(CustomEditor), typeof(CustomEditorRenderer))]
    namespace YourNameSpace.iOS
    {
        public class CustomEditorRenderer: EditorRenderer
        {
            public ChatEntryRenderer()
            {   
                UIKeyboard.Notifications.ObserveWillShow ((sender, args) => {
    
                if (Element != null)
                {
                    Element.Margin = new Thickness(0,0,0, args.FrameEnd.Height); //push the entry up to keyboard height when keyboard is activated
                }
            });
    
            UIKeyboard.Notifications.ObserveWillHide ((sender, args) => {
    
                if (Element != null)
                {
                       Element.Margin = new Thickness(0); //set the margins to zero when keyboard is dismissed
                }
    
            }); 
        }
      }
    }