简体   繁体   English

Xamarin.iOS 中显示软件键盘时如何向上移动布局

[英]How to move the layout up when the software keyboard is shown in Xamarin.iOS

I have a UITextField.我有一个 UITextField。 The soft keyboard covers the UITextField when I click.单击时,软键盘覆盖了 UITextField。
How can I move the layout up so I want it to show above the keyboard.如何向上移动布局,以便它显示在键盘上方。
Thanks in advance.提前致谢。

See a sample implimentation查看示例实现

Variables for the controller that houses the textboxes包含文本框的控制器的变量

private UIView activeview;             // Controller that activated the keyboard
private float scroll_amount = 0.0f;    // amount to scroll 
private float bottom = 0.0f;           // bottom point
private float offset = 10.0f;          // extra offset
private bool moveViewUp = false;           // which direction are we moving

Keyboard observers for ViewDidLoad(). ViewDidLoad() 的键盘观察者。

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

}

The keyboard down event is simple.键盘按下事件很简单。 If the View has been moved just reverse it.如果视图已被移动,只需将其反转即可。

private void KeyBoardDownNotification(NSNotification notification)
{
    if(moveViewUp){ScrollTheView(false);}
}

Scrolling the view will scroll the view in an animated manner.滚动视图将以动画方式滚动视图。

private void ScrollTheView(bool move)
{

    // scroll the view up or down
    UIView.BeginAnimations (string.Empty, System.IntPtr.Zero);
    UIView.SetAnimationDuration (0.3);

    RectangleF frame = View.Frame;

    if (move) {
        frame.Y -= scrollamount;
    } else {
        frame.Y += scrollamount;
        scrollamount = 0;
    }

    View.Frame = frame;
    UIView.CommitAnimations();
}

In my case, I'm using grid and stacks, wrap my root layout in a ScrollView, and set the orientation to neither.在我的例子中,我使用网格和堆栈,将我的根布局包装在 ScrollView 中,并将方向设置为两者。 And it works without any Nuget and code-behind.它无需任何 Nuget 和代码隐藏即可工作。 Actually, I tried the IQKeyboardManager, but none of it works, dependency deprecation.实际上,我尝试了 IQKeyboardManager,但没有任何效果,依赖性弃用。

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

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