在iOS中点击UITextField后应用程序崩溃

时间:2018-03-02 06:16:59

标签: ios xamarin xamarin.ios uitextfield

我在VS中使用Xamarin为iOS创建了一个应用程序。它适用于iOS模拟器和真实设备,但是一旦我收到用户的消息,该应用程序在点击UITextField后崩溃。这是登录屏幕上的默认iOS UITextField。奇怪的是,这次崩溃仅出现在iOS v6.2.1的iPhone 6s上。在iOS> = v10.3的iPhone上,此崩溃不会出现。

此UITextField只有一个事件覆盖

loginInput.EditingChanged += (s, e) =>
{
    UITextField textInput = (UITextField)s;
    if (textInput.Text.Length == 10)
    {
        textInput.ResignFirstResponder();
    }
    else if (textInput.Text.Length > 10)
    {
        char[] originalText = textInput.Text.ToCharArray();
        char[] maskedText = new char[10];
        for (int i = 0; i < 10; i++)
        {
            maskedText[i] = originalText[i];
        }
        textInput.Text = new string(maskedText);
        textInput.ResignFirstResponder();
    }
};

此外,我使用 ViewWillAppear 中的 UIKeyboard.Notifications.ObserveWillShow UIKeyboard.Notifications.ObserveWillHide 覆盖以根据键盘翻译整个视图高度。它是必需的,否则,键盘隐藏UITextField,用户无法看到他输入的内容。

public override void ViewWillAppear(bool animated)
{
    base.ViewWillAppear(animated);

    UIKeyboard.Notifications.ObserveWillShow((s, e) =>
    {
        var r = UIKeyboard.FrameEndFromNotification(e.Notification);
        keyboardHeight = r.Height;
        _originalPosition = mainPageView.Center.Y;
        if (keyboardHeight == 0)
        {
            keyboardHeight = r.Height;
        }
        Action keyboardShift = () => {
            var xpos = mainPageView.Center.X;
            var ypos = mainPageView.Center.Y - keyboardHeight;
            mainPageView.Center = new CGPoint(xpos, ypos);
        };
        UIViewPropertyAnimator propertyAnimator = new UIViewPropertyAnimator(0.25, UIViewAnimationCurve.EaseInOut, keyboardShift);
        propertyAnimator.StartAnimation();
    });
    UIKeyboard.Notifications.ObserveWillHide((s, e) =>
    {
        var r = UIKeyboard.FrameEndFromNotification(e.Notification);
        keyboardHeight = r.Height;
        if (keyboardHeight == 0)
        {
            keyboardHeight = r.Height;
        }
        Action keyboardUnShift = () => {
            var xpos = mainPageView.Center.X;
            var ypos = _originalPosition;
            mainPageView.Center = new CGPoint(xpos, ypos);
        };
        UIViewPropertyAnimator propertyAnimator = new UIViewPropertyAnimator(0.25, UIViewAnimationCurve.EaseInOut, keyboardUnShift);
        propertyAnimator.StartAnimation();
    });
}

希望有人帮助我。

0 个答案:

没有答案