xamarin - 如何在 Android 和 iOS (Xamarin Forms) 上获取键盘高度?

标签 xamarin xamarin.forms xamarin.android xamarin.ios

我想得到 Keyboard 的高度在 AndroidiOS ( Xamarin Forms )。

当屏幕显示 Portrait模式和 Landscape模式,如何获得height值(value)?

Orientations

我找到了 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/

相关文章:

android - Listview Itemclick 事件不触发 android xamarin

iOS - 在后台监听应用程序的网络连接变化

c# - 如何从一个选项卡切换到另一个选项卡 Xamarin IOS

c# - Xamarin Forms OAuth 2.0 和 Azure Active Directory - 错误 AdalServiceException : AADSTS7000218

xamarin - 检测到您的 1 个应用程序针对旧版本的 Android - Xamarin 表单 list 文件为 targetSdkVersion33

c# - MVVMCross:在 android xml 中绑定(bind)嵌套属性

c# - 如何在 Visual Studio 中为 Windows 计算机上的 Xamarin 应用程序设计用户界面?

xaml - 有没有办法让我的 BackgroundImage 均匀地填充 ContentPage? Xamarin.Forms,XAML

ios - Xamarin IOS 部署失败。 info.plist 上的 FileNotFoundException

Xamarin Forms 底部导航栏