当输入值时,Xamarin ios自动滚动视图输入点击跳转

时间:2016-06-27 15:01:19

标签: ios xamarin xamarin.ios

我已按照以下代码设置了一个视图,以便在选择输入时自动向上滚动,这样它就不会被键盘覆盖。 http://forums.xamarin.com/discussion/4994/is-there-something-in-xamarin-for-auto-scrolling-when-keyboard-show-and-hide

在我开始实际输入值之前,它很有效。当我开始在键盘上键入时,UI会移动,返回原始文本框或文本框高度的一半。

可以执行哪些强制UI移动的方法?我正在重写和调试UpdateViewConstraints()并且没有被触发,我添加的任何事件都没有。我不知道去哪里调试这个因为我无法找到移动UI的内容!

任何帮助都会很棒。我可以开始添加代码,但它几乎只是链接的副本,我不想过分混乱这个问题。

1 个答案:

答案 0 :(得分:-1)

    CoreGraphics.CGRect keyboardBounds;
    NSObject keyboardShowObserver;
    NSObject keyboardHideObserver;
   public override void ViewWillAppear(bool animated)
    { 
     base.ViewWillAppear(animated);
    keyboardShowObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, (notification) =>
        {
             NSValue nsKeyboardBounds = (NSValue)notification.UserInfo.ObjectForKey(UIKeyboard.BoundsUserInfoKey);
            keyboardBounds = nsKeyboardBounds.RectangleFValue;
            ScrollView.ContentSize = new CoreGraphics.CGSize(0, View.Frame.Height + keyboardBounds.Height);
            var Variation = FindFirstResponder(ScrollView).Frame.Bottom - View.Bounds.Height + keyboardBounds.Height;
            if (Variation > 0) { 
                            ScrollView.SetContentOffset(new CoreGraphics.CGPoint(0, Variation), false);
            }
            ScrollView.ScrollEnabled = false;
        });
 keyboardHideObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, (notification) =>
        {
            UIApplication.EnsureUIThread();
            ScrollView.ContentSize = new CoreGraphics.CGSize(0, View.Frame.Height - keyboardBounds.Height);
        });
}

public override void ViewDidDisappear(bool animated)
    {
base.ViewDidDisappear(animated);
        if (keyboardShowObserver != null)
        {
            NSNotificationCenter.DefaultCenter.RemoveObserver(keyboardShowObserver);
        }
        if (keyboardHideObserver != null)
        {
            NSNotificationCenter.DefaultCenter.RemoveObserver(keyboardHideObserver);
        }
    }