Как исправить, когда появляется клавиатура? - PullRequest
0 голосов
/ 12 января 2020

у меня есть таблица с textField в ячейке y под этой таблицей, у меня есть textField

-------------------------------------------
- --------------------------------------- -
- -        text field in table          - -
- ------------------------------------- - -
-------------------------------------------


- --------------------------------------- -
- -     other        textField          - -
- ------------------------------------- - -

, затем я сделал библиотеку для входа с клавиатуры

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

            RectangleF rectangle = (RectangleF)UIKeyboard.BoundsFromNotification(notification);
            if (currentView == null)
            {
                return;
            }

            if (activeview==null) {
                foreach (UIView view in currentView.Subviews)
                {
                    if (view.IsFirstResponder)
                    {
                        activeview = view;
                        activeview.BecomeFirstResponder();
                    }
                }
            }

            if (activeview == null)
            {
                return;
            }

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

, но когда фокус находится в текстовом поле из ячейки, этот код не работает, камера фокусируется на текстовом поле ниже.

, но этот код работает только в текстовом поле ниже, камера фокусируется на текстовом поле ниже, и это хорошо.

1 Ответ

1 голос
/ 14 января 2020

Вы можете добавить тег к belowTextfield и сравнить его с activeview, затем решите переместиться на belowTextField или tableView

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.

        UITextField belowTextField = new UITextField();

        belowTextField.Tag = 1;
    }


  public void test() {

        if (activeview == null)
        {
            foreach (UIView view in currentView.Subviews)
            {
                if (view.IsFirstResponder)
                {
                    activeview = view;
                    activeview.BecomeFirstResponder();
                }
            }
        }

        UITextField tempView = activeview as UITextField;

        if (tempView.Tag == 1)
        {
            //move up bellowTextField
        }
        else { 
            //move up tableView
        }
    }
...