Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

App crashes after tap on UITextField in iOS

$
0
0

I created an app for iOS in VS with Xamarin. It works well both on iOS Simulator and real devices, but once I get a message from the user, that app crashes after a tap on UITextField. This is default iOS UITextField on the login screen. Strange, that this crash appears only on iPhone 6s with iOS v10.2.1. On iPhone with iOS >= v10.3 this crash doesn't appear.

This UITextField has only one event overriding

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();
    }
};

Also, I use UIKeyboard.Notifications.ObserveWillShow and UIKeyboard.Notifications.ObserveWillHide in ViewWillAppear overriding to translate the whole view depending on keyboard height. It's needed because otherwise, keyboard hides UITextField and user can't see, what he types.

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();
    });
}

Hope somebody help me with this.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>