Notifications UIKeyboardEventArgs帧开始高度在第一次单击后返回0

Notifications UIKeyboardEventArgs帧开始高度在第一次单击后返回0,notifications,xamarin.ios,uikeyboard,eventargs,Notifications,Xamarin.ios,Uikeyboard,Eventargs,我有一段代码,如果文本框被键盘覆盖,我会向上滚动视图。我使用的方法样式如《Xamarin开发人员指南》中的“UIKeyboard.Notifications.ObserveWillShow”示例所示,其中回调方法为“KeyboardWillShow”。这是我的实现 public void KeyboardWillShow(UIKeyboardEventArgs KeyboardArgs, UIView uiResponderView) { if (ScrollView != nu

我有一段代码,如果文本框被键盘覆盖,我会向上滚动视图。我使用的方法样式如《Xamarin开发人员指南》中的“UIKeyboard.Notifications.ObserveWillShow”示例所示,其中回调方法为“KeyboardWillShow”。这是我的实现

public void KeyboardWillShow(UIKeyboardEventArgs KeyboardArgs, UIView uiResponderView)
    {
    if (ScrollView != null)
            {
                if (uiResponderView != null)
                {
                    UIEdgeInsets contentInsets = new UIEdgeInsets(0.0f, 0.0f, KeyboardArgs.FrameBegin.Height, 0.0f);
                    ScrollView.ContentInset = contentInsets;
                    ScrollView.ScrollIndicatorInsets = contentInsets;

                    CGRect tableViewRect = ScrollView.Frame;
                    tableViewRect.Height -= KeyboardArgs.FrameBegin.Height;

                    if (!tableViewRect.Contains(uiResponderView.Frame.Location))
                    {
                        ScrollView.ScrollRectToVisible(uiResponderView.Frame, true);
                    }
                }
            }
    }
我还使用Xamarin的开发者指南中的“UIKeyboard.Notifications.ObserveWillHide”示例来监听键盘何时隐藏,其中回调方法是“KeyboardWillHide”。这是我对它的实现

public void KeyBoardWillHide(object sender, UIKeyboardEventArgs args)
    {
        ScrollView.ContentInset = UIEdgeInsets.Zero;
        ScrollView.ScrollIndicatorInsets = UIEdgeInsets.Zero;
    }
所有这些第一次都可以正常工作,但随后每次KeyboardArgs.FrameBegin.Height都返回0。有人能告诉我我错过了什么吗

编辑: 我还应该注意到,在viewwill消失时,我处理掉了观察者

解决方案: 根据Kevin的笔记,我将我的“KeyboardWillShow”事件更改为使用“KeyboardArgs.FrameEnd.Height”而不是“KeyboardArgs.FrameBegin.Height”,该过程可以正常工作。该事件现在看起来像:

public void KeyboardWillShow(UIKeyboardEventArgs KeyboardArgs, UIView uiResponderView)
{
    if (ScrollView != null)
    {
        if (uiResponderView != null)
        {
            UIEdgeInsets contentInsets = new UIEdgeInsets(0.0f, 0.0f, KeyboardArgs.FrameEnd.Height, 0.0f);
            ScrollView.ContentInset = contentInsets;
            ScrollView.ScrollIndicatorInsets = contentInsets;

            CGRect tableViewRect = ScrollView.Frame;
            tableViewRect.Height -= KeyboardArgs.FrameEnd.Height;

            if (!tableViewRect.Contains(uiResponderView.Frame.Location))
            {
                ScrollView.ScrollRectToVisible(uiResponderView.Frame, true);
            }
        }
    }
}
解决方案:

使用FrameEnd.Height而不是FrameBegin.Height

参考资料:

UIKeyboardFrameBeginUserInfo键

NSValue对象的键,该对象包含一个CGRect,该CGRect在屏幕坐标中标识键盘的起始帧矩形。框架矩形反映设备的当前方向

UIKeyboardFrameEndUserInfoKey

NSValue对象的键,该对象包含一个CGRect,该CGRect在屏幕坐标中标识键盘的结束帧矩形。框架矩形反映设备的当前方向

苹果的文档:

其他有关个案: