键盘在UIViewController Xamarin IOS中隐藏了UITextView

问题描述 投票:3回答:1

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

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

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

Showing UITextView.

Keyboard is hiding my textview field.

ios uiviewcontroller xamarin.ios keyboard uitextview
1个回答
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/

© www.soinside.com 2019 - 2024. All rights reserved.