键盘出现时如何解决?

问题描述 投票:0回答:1

我在该表下的单元格y中有一个带有textField的表,我有一个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;
        }

但是当焦点位于该单元格的文本字段中时,此代码不起作用,相机将聚焦在下面的文本字段上。

但是此代码仅在下面的文本字段中有效,相机将焦点对准下面的文本字段,这很好。

c# ios xamarin xamarin.ios
1个回答
0
投票

您可以将标签添加到belowTextfield并将其与activeview进行比较,然后决定向上移动belowTextFieldtableView

  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
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.