4
votes

I a seeing a strange behavior or may be totally wrong they way I do it.

Got a page with an Editor at the bottom with a button and a listview with some messages at the top.

I am using the keyboardRenderer in this link

https://xamgirl.com/adjusting-elements-when-keyboard-shows-in-xamarin-forms/

Problem

When I start typing the keyboard appears and the editor totally collapses and start typing and it let me types but the it collapses (see pic) Also at times collapses and cannot see anything.

How do you use an editor with keyboard in iOS

Collapsed

enter image description here

Not collapsed enter image description here

Renderer used

[assembly: ExportRenderer(typeof(KeyboardView), 
typeof(KeyboardViewRenderer))]
namespace KeyboardSample.iOS.Renderers
{
public class KeyboardViewRenderer : ViewRenderer
{
    NSObject keyboardShowObserver;
    NSObject keyboardHideObserver;
    protected override void OnElementChanged(ElementChangedEventArgs<View> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            RegisterForKeyboardNotifications();
        }

        if (e.OldElement != null)
        {
            UnregisterForKeyboardNotifications();
        }
    }

    private void RegisterForKeyboardNotifications()
    {
        if (keyboardShowObserver == null)
            keyboardShowObserver = UIKeyboard.Notifications.ObserveWillShow(OnKeyboardShow);
        if (keyboardHideObserver == null)
            keyboardHideObserver = UIKeyboard.Notifications.ObserveWillHide(OnKeyboardHide);
    }

    private void OnKeyboardShow(object sender, UIKeyboardEventArgs args)
    {
        NSValue result = (NSValue)args.Notification.UserInfo.ObjectForKey(new NSString(UIKeyboard.FrameEndUserInfoKey));
        CGSize keyboardSize = result.RectangleFValue.Size;
        if (Element != null)
        {
            Element.Margin = new Thickness(0, 0, 0, keyboardSize.Height); //push the entry up to keyboard height when keyboard is activated

        }
    }

    private void OnKeyboardHide(object sender, UIKeyboardEventArgs args)
    {
        if (Element != null)
        {
            Element.Margin = new Thickness(0); //set the margins to zero when keyboard is dismissed
        }

    }


    private void UnregisterForKeyboardNotifications()
    {
        if (keyboardShowObserver != null)
        {
            keyboardShowObserver.Dispose();
            keyboardShowObserver = null;
        }

        if (keyboardHideObserver != null)
        {
            keyboardHideObserver.Dispose();
            keyboardHideObserver = null;
        }
    }
}

public class KeyboardView: Grid
{

}

In My Page

<controls:KeyboardView Padding="0,60,0,0"
                        VerticalOptions="FillAndExpand">
    <Grid RowSpacing="0" ColumnSpacing="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <ListView Grid.Row="0">
            <!--TODO -->
        </ListView>

        <Grid Grid.Row="1" RowSpacing="0" ColumnSpacing="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

                <StackLayout   Orientation="Horizontal">
                    <ScrollView HorizontalOptions="FillAndExpand" >
                        <Editor  
                                AutoSize="TextChanges"
                                Text="{Binding MyText, Mode=TwoWay}" 
                                Margin="10"  
                                TextColor="Black" 
                                Keyboard="Chat"  
                                BackgroundColor="Gainsboro"
                                HorizontalOptions="FillAndExpand" 
                                VerticalOptions="FillAndExpand"
                                PlaceholderColor="LightGray"
                                Placeholder="Type your message here" />
                    </ScrollView>
                    <StackLayout HorizontalOptions="End" VerticalOptions="End">
                        <Button Text="Send"  Command="{Binding TODOCommand}"  Margin="0,10,10,10"/>
                    </StackLayout>
                </StackLayout>
        </Grid>
    </Grid>
</controls:KeyboardView>
1
Have you downloaded the demo and have a test? - Lucas Zhang
Can you provide your sample so that I can test it on my side. - Lucas Zhang
@LucasZhang-MSFT uploaded a sample proj github.com/developer9969/KeyboardSpike - developer9969
I will check it. - Lucas Zhang
@LucasZhang-MSFT very grateful! - developer9969

1 Answers

1
votes

Removing the scrollview sorted the problem