How to prevent Editor to go behind the keyboard in Xamarin.Forms?

后端 未结 3 1919
悲哀的现实
悲哀的现实 2021-01-22 18:39

I have a Chat app. Currently, there is an Entry control for add chat text. Now I want to provide multiline Entry, same like Whatsapp.

  • If user type more than one li
相关标签:
3条回答
  • 2021-01-22 19:18

    For Ios there is one plugin. You can use that. The link is Here.

    For Andorid you have to just set below code in MainActivity after LoadApplication(new App()) method.

    App.Current.On<Xamarin.Forms.PlatformConfiguration.Android().
    UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
    

    Updated answer for iOS :

    For IOS you can use the following custom renderer to solve the keyboard overlapping issue. And please removed the keyboardoverlap nuget package from the project.

    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
                    }
    
                }); 
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-22 19:27

    Try this renderer for ios.

    using System;  
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.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
                    }
    
                }); 
            }
        }
    }
    

    for android add this in MainActivity

     App.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().
        UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
    
    0 讨论(0)
  • 2021-01-22 19:27

    You can try to change this line:

    <local:ChatEditorWithPlaceholder  x:Name="txtMessage" Grid.Column="0"  TextChanged="EnableSend" Text="{Binding OutGoingText}"/>
    

    For this one:

    <Editor x:Name="txtMessage" Grid.Column="0" AutoSize="TextChanges" TextChanged="EnableSend" Text="{Binding OutGoingText}"/>
    
    0 讨论(0)
提交回复
热议问题