键盘在UIViewController Xamarin IOS中隐藏了UITextView

时间:2016-12-13 18:23:17

标签: ios uiviewcontroller xamarin.ios keyboard uitextview

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

我想在我的UITextView之下放一些UIViewController。 我只在我的应用程序中使用横向模式。 我正在使用Xamarin IOS来制作这个应用程序。

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

Showing UITextView.

Keyboard is hiding my textview field.

1 个答案:

答案 0 :(得分:2)

您需要在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/