键盘在UIViewController Xamarin IOS中隐藏了UITextView

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了键盘在UIViewController Xamarin IOS中隐藏了UITextView相关的知识,希望对你有一定的参考价值。

我在UIViewController上创建了一个UITextView并放置了一个标签和UIViewController

我想把一些UITextViews放在我的UIViewController上。我只在我的应用程序中使用横向模式。我正在使用Xamarin ios来制作这个应用程序。

屏幕下方显示正在发生的事情!有人可以帮帮我吗!

Showing UITextView.

Keyboard is hiding my textview field.

答案

您需要在viewcontroller中添加观察者,如下所示出现此问题。

键盘观察员为ViewDidLoad()

// Keyboard popup
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.DidShowNotification,KeyBoardUpNotification);

// Keyboard Down
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.WillHideNotification,KeyBoardDownNotification);

// Keyboard popup
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.DidShowNotification,KeyBoardUpNotification);

// Keyboard Down
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.WillHideNotification,KeyBoardDownNotification);

首先是KeyboardUpNotification方法。基本上你计算控件是否会被键盘隐藏,如果是这样,计算需要移动多少视图来显示控件,然后移动它。

private void KeyBoardUpNotification(NSNotification notification)
{
    // get the keyboard size
    RectangleF r = UIKeyboard.BoundsFromNotification (notification);

    // Find what opened the keyboard
    foreach (UIView view in this.View.Subviews) {
        if (view.IsFirstResponder)
            activeview = view;
    }

    // Bottom of the controller = initial position + height + offset      
    bottom = (activeview.Frame.Y + activeview.Frame.Height + offset);

    // Calculate how far we need to scroll
    scroll_amount = (r.Height - (View.Frame.Size.Height - bottom)) ;

    // Perform the scrolling
    if (scroll_amount > 0) {
         moveViewUp = true;
         ScrollTheView (moveViewUp);
    } else {
         moveViewUp = false;
    }

}

private void KeyBoardUpNotification(NSNotification notification)
{
    // get the keyboard size
    RectangleF r = UIKeyboard.BoundsFromNotification (notification);

    // Find what opened the keyboard
    foreach (UIView view in this.View.Subviews) {
        if (view.IsFirstResponder)
            activeview = view;
    }

    // Bottom of the controller = initial position + height + offset      
    bottom = (activeview.Frame.Y + activeview.Frame.Height + offset);

    // Calculate how far we need to scroll
    scroll_amount = (r.Height - (View.Frame.Size.Height - bottom)) ;

    // Perform the scrolling
    if (scroll_amount > 0) {
         moveViewUp = true;
         ScrollTheView (moveViewUp);
    } else {
         moveViewUp = false;
    }
}

活动字段用于跟踪当前启动的文本字段。

public override void EditingStarted (UITextField textField)
{
    activeview = textField;
}

更多:http://www.gooorack.com/2013/08/28/xamarin-moving-the-view-on-keyboard-show/

以上是关于键盘在UIViewController Xamarin IOS中隐藏了UITextView的主要内容,如果未能解决你的问题,请参考以下文章

键盘存在时 UIViewController 向上移动

当使用 Swift 出现键盘时如何向上移动 UIViewController 的内容

关闭在 UINavigationController 堆栈中的 UIViewController 的键盘

键盘在UIViewController Xamarin IOS中隐藏了UITextView

键盘显示时在 UIViewController 中设置 Tableview y 位置

键盘遮住了我的 UIViewController?