UIKeyboardEventArgs FrameBegin 高度 returns 0 after first click
UIKeyboardEventArgs FrameBegin Height returns 0 after first click
我有一些代码,如果文本框被键盘覆盖,我会向上滚动视图。我使用的方法样式如 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" returns 0。有人可以告诉我我缺少什么吗?
编辑:
我还应该注意,在 "ViewWillDisappear" 上,我处理了观察者。
解决方案:
根据 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
。
参考文献:
UIKeyboardFrameBeginUserInfoKey
The key for an NSValue object containing a CGRect that identifies the starting frame rectangle of the keyboard in screen coordinates. The frame rectangle reflects the current orientation of the device.
UIKeyboardFrameEndUserInfoKey
The key for an NSValue object containing a CGRect that identifies the ending frame rectangle of the keyboard in screen coordinates. The frame rectangle reflects the current orientation of the device.
Apple 的文档:
https://developer.apple.com/documentation/uikit/uikeyboardframebeginuserinfokey
https://developer.apple.com/documentation/uikit/uikeyboardframeenduserinfokey
其他相关案例:
我有一些代码,如果文本框被键盘覆盖,我会向上滚动视图。我使用的方法样式如 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" returns 0。有人可以告诉我我缺少什么吗?
编辑: 我还应该注意,在 "ViewWillDisappear" 上,我处理了观察者。
解决方案: 根据 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
。
参考文献:
UIKeyboardFrameBeginUserInfoKey
The key for an NSValue object containing a CGRect that identifies the starting frame rectangle of the keyboard in screen coordinates. The frame rectangle reflects the current orientation of the device.
UIKeyboardFrameEndUserInfoKey
The key for an NSValue object containing a CGRect that identifies the ending frame rectangle of the keyboard in screen coordinates. The frame rectangle reflects the current orientation of the device.
Apple 的文档: https://developer.apple.com/documentation/uikit/uikeyboardframebeginuserinfokey https://developer.apple.com/documentation/uikit/uikeyboardframeenduserinfokey
其他相关案例: