首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当键盘出现时,如何修复?

当键盘出现时,如何修复?
EN

Stack Overflow用户
提问于 2020-01-12 20:33:21
回答 1查看 99关注 0票数 0

我有一个表,在这个表下面的单元格y中有一个textField。我有一个textField

代码语言:javascript
复制
-------------------------------------------
- --------------------------------------- -
- -        text field in table          - -
- ------------------------------------- - -
-------------------------------------------


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

然后我做了一个用于键盘入口的库

代码语言:javascript
复制
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;
        }

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

但这段代码只能在下面的文本字段中工作,相机聚焦在下面的textfield上,这很好。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-14 11:02:48

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

代码语言:javascript
复制
    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
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59703833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档