【问题标题】:How to move the layout up when the software keyboard is shown in Xamarin.iOS如何在 Xamarin.iOS 中显示软件键盘时向上移动布局
【发布时间】:2017-02-09 06:49:44
【问题描述】:

我有一个 UITextField。当我单击时,软键盘会覆盖 UITextField。
如何将布局向上移动,使其显示在键盘上方。
提前致谢。

【问题讨论】:

    标签: ios user-interface xamarin xamarin.ios ios-keyboard-extension


    【解决方案1】:

    【讨论】:

    • 太棒了——很高兴他们创造了这个
    【解决方案2】:

    查看示例实现

    包含文本框的控制器的变量

    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
    

    ViewDidLoad() 的键盘观察器。

    // 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 KeyBoardDownNotification(NSNotification notification)
    {
        if(moveViewUp){ScrollTheView(false);}
    }
    

    滚动视图将以动画方式滚动视图。

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

    【讨论】:

    • 您好,我使用了您的解决方案,但遇到了问题。我计算出的 scroll_amount 总是负数...知道我做错了什么...谢谢
    • @Christoph 此帖来自:gooorack.com/2013/08/28/…也许会对你有所帮助
    【解决方案3】:

    在我的例子中,我使用网格和堆栈,将我的根布局包装在 ScrollView 中,并将方向设置为两者都没有。它可以在没有任何 Nuget 和代码隐藏的情况下工作。 实际上,我尝试了 IQKeyboardManager,但它都不起作用,依赖弃用。

    【讨论】:

      猜你喜欢
      • 2010-12-30
      • 2012-02-25
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-20
      • 2011-04-16
      • 1970-01-01
      相关资源
      最近更新 更多