当页面在Xamarin Forms中已经具有滚动视图时,如何处理iOs键盘重叠条目 [英] How to deal with iOs keyboard overlapping entry when the page already has a scrollview in Xamarin Forms

查看:94
本文介绍了当页面在Xamarin Forms中已经具有滚动视图时,如何处理iOs键盘重叠条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS键盘上遇到问题.我正在跨平台的Xamarin上开发一个聊天页面,并且该页面具有scrollView,使用户可以滚动查看消息.

I am having a problem regarding the keyboard on iOS. I am developing a chat page on Xamarin, cross platform, and this page has a scrollView which make possible to the user to scroll along the messages.

有关iOS键盘的常见问题,涵盖了条目. iOS不会自动向上滚动页面.为了解决这个问题,一个简单的解决方案是放置一个标记"Scrollview",覆盖页面的整个代码.这通常可以正常工作.但是,我的页面内部已经有一个滚动视图.因此,当我将一个滚动视图放在另一个滚动视图中时,即使在Android上,该行为也有些疯狂.有时它滚动消息视图",有时它滚动整个页面.

There is a commom problem regarding the keyboard on iOS, where it covers the entry. iOS doesn't scroll up the page automatically. To solve this problem, a simple solution is to put a tag "Scrollview" covering the whole code of the page. This usually works fine. However, my page already has a scrollview inside it. Therefore, when I put a scrollview inside another scrollview, the behaviour is a little crazy, even on Android. Sometimes it scrolls the "messages view", sometimes it scrolls the whole page.

有一种解决方案可以避免iOS上不使用scrollview标签的键盘问题?还是有一种在另一个滚动视图中使用滚动视图的解决方案?

There is a solution to avoid the keyboard problem on iOS not using the scrollview tag? Or there is a solution to use a scrollview inside another scrollview?

有什么建议吗?

提前谢谢!

推荐答案

在尝试在我们的应用中实现聊天功能时,我遇到了相同的问题.一种解决方案是对Entry使用自定义渲染器,并在触发键盘事件时调整其边距.我遵循的模式在此信息中设置.

I encountered the same problem while trying to implement a chat feature in our app. A solution is to use a custom renderer for the Entry, and adjust its margin when the keyboard event is triggered. I followed the pattern set in this post.

在这种情况下,我将Entry嵌入到ContentView中,所以我继承了ViewRenderer来调整ContentView.根据情况,您继承的渲染器可能会有所不同.但是,在大多数情况下,您应该能够将边距重置为键盘的高度.

In this case, I embed my Entry into a ContentView, so I inherit from a ViewRenderer to adjust the ContentView. Depending on the situation, the Renderer you inherit from may vary. However, in most cases you should be able to reset the margin to the Keyboard's height.

using System;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using HBIClientFacingApp;
using HBIClientFacingApp.iOS;

[assembly:ExportRenderer( typeof(ChatViewWithEntry), typeof(ChatEntryRenderer))]
namespace YourNameSpace.iOS
{
    public class ChatEntryRenderer : ViewRenderer //Depending on your situation, you will need to inherit from a different renderer
    {
        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
                }

            }); 
        }

    }
}

这篇关于当页面在Xamarin Forms中已经具有滚动视图时,如何处理iOs键盘重叠条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆