首页 > 解决方案 > 出现键盘时带有滚动的 ViewController

问题描述

我想看到我所有的视图,但是当出现键盘时我看不到所有视图,例如:

-------------------------------------------
- --------------------------------------- -    i want what my viewcontroller have scroll
- -        text field 1                 - -
- -        text field 2                 - -
- -        text field n                 - -
- ------------------------------------- - -
-------------------------------------------
-                                         -
-               keyboard                  -
-                                         - 
- -                                     - -
- ------------------------------------- - -

这是我的代码,意思是我可以在我的文本字段中修复此代码,我正在使用这个 IsFirstResponder 来查看当前文本字段,但我想查看我的所有文本字段,我可以放一个滚动条吗? :

可以放卷轴吗?我检查了许多应用程序,我认为这是可能的。

public static void KeyBoardUpNotification(NSNotification notification)
        {
            scrollamount = 0.0f;

            RectangleF rectangle = (RectangleF)UIKeyboard.BoundsFromNotification(notification);


            bottom = ((float)(activeview.Frame.Y + activeview.Frame.Height + offset));
            scrollamount = ((float)(rectangle.Height - (currentView.Frame.Size.Height - bottom)));

            if (scrollamount != 0)
            {
                moveViewUp = true;
                ScrollTheView(moveViewUp);
            }
            else
            {
                moveViewUp = false;
            }
        }

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

        private static void ScrollTheView(bool move)
        {
            UIView.BeginAnimations(string.Empty, IntPtr.Zero);
            UIView.SetAnimationDuration(0.3);
            RectangleF frame = (RectangleF)currentView.Frame;

            if (move)
            {
                frame.Y = y;
                frame.Y -= scrollamount;    
            }
            else
            {
                frame.Y = y;
                scrollamount = 0;
                moveViewUp = false;
            }
            currentView.Frame = frame;
            UIView.CommitAnimations();
            scrollamount = 0;
            frame.Y = 0;

标签: c#iosxamarin

解决方案


您可以使用来自 nuget 的插件Xamarin.IQKeyboardManager

用法

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();

    Xamarin.IQKeyboardManager.SharedManager.EnableAutoToolbar = true;
    Xamarin.IQKeyboardManager.SharedManager.ShouldResignOnTouchOutside = true;
    Xamarin.IQKeyboardManager.SharedManager.ShouldToolbarUsesTextFieldTintColor = true;
    Xamarin.IQKeyboardManager.SharedManager.KeyboardDistanceFromTextField = 300f;
    //...
}

有关更多详细信息,您可以查看https://github.com/TheEightBot/Xamarin.IQKeyboardManager


推荐阅读