简体   繁体   English

键盘在UIViewController Xamarin IOS中隐藏了UITextView

[英]Keyboard is hiding UITextView in UIViewController Xamarin IOS

I created a UIViewController and Put a Label and UITextView on UIViewController . 我创建了一个UIViewController ,把一个标签和UITextViewUIViewController

I want to put some UITextView s on my UIViewController one under another. 我想在我的UIViewController上放一些UITextView I am using Landscape mode only in my App. 我只在我的应用程序中使用横向模式。 I am using Xamarin IOS for making this app. 我正在使用Xamarin IOS来制作这个应用程序。

Below screen shows what is happening! 屏幕下方显示正在发生的事情! Can someone please help me! 有人可以帮帮我吗!

显示UITextView。

键盘隐藏了我的textview字段。

You need to add observer in your viewcontroller where this issue occurs as below . 您需要在viewcontroller中添加观察者,如下所示出现此问题。

Keyboard observers for ViewDidLoad() . 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);

First up is the KeyboardUpNotification method. 首先是KeyboardUpNotification方法。 Essentially you calculate if the control will be hidden by the keyboard and if so calculate how much the view needs to be moved to show the control, and then move it. 基本上你计算控件是否会被键盘隐藏,如果是这样,计算需要移动多少视图来显示控件,然后移动它。

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;
    }
}

Active field is use for track your currently started textfield. 活动字段用于跟踪当前启动的文本字段。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM