键盘与iOS上Xamarin.Forms中的编辑器重叠

时间:2018-10-31 07:56:17

标签: ios xaml xamarin.forms uikeyboard

当我将焦点放在Editor上时,键盘与EditorButton重叠。我知道,有一个解决方案:将元素放在ScrollView内,但是此解决方案在ListView内滚动ScrollView时会产生很大的问题。我应该怎么做才能使其正常工作?

布局: 红色-ScrollView, 绿色-ListView, 蓝色-StackLayoutEditorButton

enter image description here

以下是视频的工作原理: https://streamable.com/wvd3h

解决方案2是使用Xamarin.Forms.Plugins-KeyboardOverlap,但是此插件的TabbedPage上的选项卡栏有问题

1 个答案:

答案 0 :(得分:0)

您可以创建一个自定义的iOS渲染器,而不是使用滚动视图,当键盘处于活动状态时,该渲染器会向上移动视图(使用边距)。

在此可以看到一个示例: https://github.com/rdelrosario/ChatUIXForms/blob/master/ChatUIXForms.iOS/Renderers/ChatEntryRenderer.cs

相关代码:

void RegisterForKeyboardNotifications()
    {
        if (_keyboardShowObserver == null)
            _keyboardShowObserver = UIKeyboard.Notifications.ObserveWillShow(OnKeyboardShow);
        if (_keyboardHideObserver == null)
            _keyboardHideObserver = UIKeyboard.Notifications.ObserveWillHide(OnKeyboardHide);
    }

    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

        }
    }

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

    }


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

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

在Android上,这可能不是问题,但如果是这样,您可以尝试

android:Application.WindowSoftInputModeAdjust="Resize"