我想得到 Keyboard
的高度在 Android
和 iOS
( Xamarin Forms
)。
当屏幕显示 Portrait
模式和 Landscape
模式,如何获得height
值(value)?
我找到了 Swift
的引用文献在 iOS
:
What is the height of iPhone's onscreen keyboard?
How to get height of Keyboard?
但是可以在Xamarin
上给我提供源代码
以及如何在 Android
上获取键盘的高度?
请帮我!
最佳答案
对于 iOS
有两种方法可以做到这一点!
第一种方式 :为一个 UIViewController 实现它,如果你的应用程序很小,如下:
步骤 1:为显示和隐藏观察者添加字段:
private NSObject _keyboardObserverWillShow;
private NSObject _keyboardObserverWillHide;
第 2 步:更新 ViewDidLoad 和 ViewDidUnload 覆盖以在键盘显示或隐藏时添加/删除观察者:public override void ViewDidLoad() {
base.ViewDidLoad ();
_keyboardObserverWillShow = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.DidShowNotification, KeyboardDidShowNotification);
_keyboardObserverWillHide = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, KeyboardWillHideNotification);
}
public override void ViewDidUnload() {
base.ViewDidUnload();
NSNotificationCenter.DefaultCenter.RemoveObserver(_keyboardObserverWillShow);
NSNotificationCenter.DefaultCenter.RemoveObserver(_keyboardObserverWillHide);
}
第 3 步:然后填充函数 KeyboardDidShowNotification
& KeyboardWillHideNotification
为每个观察者执行。它相当长,需要我花一些时间来解释每个部分,但你可以在评论中问我。它是这样的:private void KeyboardWillHideNotification (NSNotification notification)
{
UIView activeView = View.FindFirstResponder();
if (activeView == null)
return;
UIScrollView scrollView = activeView.FindSuperviewOfType (this.View, typeof(UIScrollView)) as UIScrollView;
if (scrollView == null)
return;
// Reset the content inset of the scrollView and animate using the current keyboard animation duration
double animationDuration = UIKeyboard.AnimationDurationFromNotification(notification);
UIEdgeInsets contentInsets = new UIEdgeInsets(0.0f, 0.0f, 0.0f, 0.0f);
UIView.Animate(animationDuration, delegate{
scrollView.ContentInset = contentInsets;
scrollView.ScrollIndicatorInsets = contentInsets;
});
}
private void KeyboardDidShowNotification (NSNotification notification)
{
UIView activeView = View.FindFirstResponder();
if (activeView == null)
return;
((UITextField)activeView).ShowDoneButtonOnKeyboard();
UIScrollView scrollView = activeView.FindSuperviewOfType(this.View, typeof(UIScrollView)) as UIScrollView;
if (scrollView == null)
return;
RectangleF keyboardBounds = UIKeyboard.BoundsFromNotification(notification);
UIEdgeInsets contentInsets = new UIEdgeInsets(0.0f, 0.0f, keyboardBounds.Size.Height, 0.0f);
scrollView.ContentInset = contentInsets;
scrollView.ScrollIndicatorInsets = contentInsets;
// If activeField is hidden by keyboard, scroll it so it's visible
RectangleF viewRectAboveKeyboard = new RectangleF(this.View.Frame.Location, new SizeF(this.View.Frame.Width, this.View.Frame.Size.Height - keyboardBounds.Size.Height));
RectangleF activeFieldAbsoluteFrame = activeView.Superview.ConvertRectToView(activeView.Frame, this.View);
// activeFieldAbsoluteFrame is relative to this.View so does not include any scrollView.ContentOffset
// Check if the activeField will be partially or entirely covered by the keyboard
if (!viewRectAboveKeyboard.Contains(activeFieldAbsoluteFrame)) {
// Scroll to the activeField Y position + activeField.Height + current scrollView.ContentOffset.Y - the keyboard Height
PointF scrollPoint = new PointF(0.0f, activeFieldAbsoluteFrame.Location.Y + activeFieldAbsoluteFrame.Height + scrollView.ContentOffset.Y - viewRectAboveKeyboard.Height);
scrollView.SetContentOffset(scrollPoint, true);
}
}
第二种方式 : 创建 keyboard handler您可以通过您的 BaseViewController 用于每个页面对于安卓:
您可以按照 stackOverflow Xamarin solution here 中提到的解决方案进行操作。
((Activity)Forms.Context).Window.SetSoftInputMode(SoftInput.AdjustPan);
.有时这不起作用,所以你可以 use this to fix it . PS:没有复制这些答案,因为重写答案没有意义。
关于xamarin - 如何在 Android 和 iOS (Xamarin Forms) 上获取键盘高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58350967/