ios - 键盘在 UIViewController Xamarin IOS 中隐藏 UITextView

标签 ios uiviewcontroller xamarin.ios keyboard uitextview

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

我想将一些 UITextView 放在我的 UIViewController 上,一个放在另一个下面。 我仅在我的应用程序中使用横向模式。 我正在使用 Xamarin IOS 来制作这个应用程序。

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

Showing UITextView.

Keyboard is hiding my textview field.

最佳答案

您需要在发生此问题的 View Controller 中添加观察者,如下所示。

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 方法。本质上,您计算控件是否会被键盘隐藏,如果是,则计算 View 需要移动多少才能显示控件,然后移动它。

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/

关于ios - 键盘在 UIViewController Xamarin IOS 中隐藏 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41128016/

相关文章:

ios - 在重复的 UIViewsController 之间定位特定的 UIViewController 实例

ios - 在 Swift 中为 UIViewController 自定义初始化,在 Storyboard 中设置界面

iphone - 是否可以使用 Microsoft 管理的 EWS 通过 MonoTouch 构建 iPhone 应用程序?

c# - MonoTouch 中的 ViewForZoomingInScrollView

ios - 如何让 UIButton 调用目标类并 Hook 此方法? (使用theos编译越狱tweak)

关闭模态后 iOS TableView 重新加载

ios - iPhone OpenGL ES 应用程序中的屏幕键盘方向问题

ios - 如何从 UItableviewcell 呈现一个 viewcontroller

ios - 在 iOS 中发布 UIViewController 的最佳方式是什么

c# - OxyPlot:如何隐藏左侧和顶部轴线