c# - 键盘出现时如何修复?

标签 c# ios xamarin xamarin.ios

我有一个表,在这个表下的单元格 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;
        }

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

但此代码仅适用于下方的文本字段,相机会聚焦在下方的文本字段上,这很好。

最佳答案

可以在textfield下方添加标签,与activeview比较,再决定是上移textField下方还是 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
        }
    }

关于c# - 键盘出现时如何修复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59703833/

相关文章:

xamarin - 如何将入口参数传递给 Xamarin MVVM viewmodel

c# - 我们是否可以在启用 Mono 解释器的情况下发布 Xamarin iOS 应用程序?

c# - 如何从终结器中正确处理非托管资源的集合?

c# - 如何在子类方法中显示父类成员的正确值

ios - 向用户显示替代付款方式。

objective-c - wait_fences : failed to receive reply: 10004003 error still appearing

ios - Xamarin iOS - 我们如何以编程方式将辅助功能焦点设置为按钮

c# - 如何修复网站上的 Gecko 29.0 错误 (sec_error_unknown_issuer)?

C# Winforms 连接到 MySQL AWS 数据库

objective-c - MobFox 总是给我回复 "No inventory for ad request"