簡體   English   中英

出現鍵盤時如何解決?

[英]How fix when the keyboard appears?

我在這張表格下的單元格 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;
        }

但是當焦點位於單元格的文本字段中時,此代碼不起作用,相機會聚焦在下面的文本字段上。

但此代碼僅適用於下面的文本字段,相機聚焦於下面的文本字段,這很好。

您可以將標簽添加到belowTextfield並將其與activeview進行比較,然后決定向上移動belowTextFieldtableView

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM