UITextField text jumps

IosSwiftUitextfield

Ios Problem Overview


I have ViewController with 2 UITextField elements: Login and Password. I set delegate for these fields, which includes code below:

func textFieldShouldReturn(textField: UITextField) -> Bool {
    if textField === self.loginField {
        self.loginField.resignFirstResponder()
        self.passwordField.becomeFirstResponder()
        return false
    }
    
    return true
}

This logic should switch user from login text field to password when he presses Next button on keyboard. But I stuck with glitch: after

self.passwordField.becomeFirstResponder()

text in login field jumps to the top left corner and back. And what's more strange: this glitch reproduces only first time, then you need recreate ViewController to observe this behavior

Here is video of the glitch http://tinypic.com/player.php?v=6nsemw%3E&s=8#.VgVb3cuqpHx

I ended up with this:

func textFieldShouldReturn(textField: UITextField) -> Bool {
    if textField === self.loginField {
        self.loginField.resignFirstResponder()
        // Shitty workaround. Hi, Apple!
        self.loginField.setNeedsLayout()
        self.loginField.layoutIfNeeded()
        
        self.passwordField.becomeFirstResponder()
        return false
    }
    
    return true
}

Ios Solutions


Solution 1 - Ios

Based on some of the other ideas posted here, this is a solution that is easy to implement, works (for me) in all cases, and doesn't appear to have any side effects:

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    // Workaround for the jumping text bug.
    [textField resignFirstResponder];
    [textField layoutIfNeeded];
}

This solution works both if you're going to the next field programmatically from -textFieldShouldReturn: or if the user just touches another responder.

Solution 2 - Ios

In a UITextField subclass you can do the following:

-(BOOL)resignFirstResponder
{
    BOOL resigned = [super resignFirstResponder];
    [self layoutIfNeeded];
    return resigned;
}

The trick here is to make sure you call layoutIfNeeded after resignFirstResponder has been called.

Doing it this way is quite handy because you don't need to call resignFirstResponder in the delegate callbacks yourself as this caused me problems inside a UIScrollView, the above however doesn't :)

Solution 3 - Ios

func textFieldDidEndEditing(_ textField: UITextField) {
    
    textField.layoutIfNeeded()
}

Solution 4 - Ios

Use this code to avoid the jumping of text in UITextField.

- (BOOL) textFieldShouldReturn:(UITextField *)textField{

      if(textField == self.txtUserName){
        [self.txtEmail becomeFirstResponder];
      }
      else if (textField == self.txtEmail){
        [self.txtPassword becomeFirstResponder];
      }
      else if (textField == self.txtPassword){
        [self.txtConfirmPassword becomeFirstResponder];
      }
      else if (textField == self.txtConfirmPassword){
        [self.txtFirstName becomeFirstResponder];
      }
      else{
        [textField resignFirstResponder];
      }

   return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
   [textField resignFirstResponder];
   [textField layoutIfNeeded];
}

Solution 5 - Ios

I'm also facing the same problem. The below code is worked for me.

func textFieldDidEndEditing(_ textField: UITextField) {
   
    textField.layoutIfNeeded()
}

Solution 6 - Ios

More "generic" option is to use notifications right inside your UITextField subclass:

 - (void)setupJumpingTextWorkaround {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(forceLayout)
                                                 name:UITextFieldTextDidEndEditingNotification object:self];
}

- (void)forceLayout {
        [self setNeedsLayout];
        [self layoutIfNeeded];
    }

Do not forget to unsubscribe

Solution 7 - Ios

Based on what I understand from this:

This issue can be caused when you have layout changes or animations handled in the callbacks for keyboard will show and hide notifications (usually in cases when you want the textfield to be pushed up so the keyboard won't hide it).

Solution: I was facing this problem as I was doing layoutIfNeeded every time keyboard will show got called assuming its safe, obviously its not, so when I put a check to do that only when there is a need for changing the frames, the jump stopped.

Solution 8 - Ios

I don't give my text fields delegates. Instead I create an IBAction and attach it to the "Did End On Exit" event. The glitch happens with this method too, but only in iOS 9. It looks to be an OS bug.

My action looks like this:

@IBAction func textFieldAction(sender: UITextField) {
	if sender === usernameField {
		passwordField.becomeFirstResponder()
	}
}

With the above, the glitch happens, but when I do the below the glitch goes away:

@IBAction func textFieldAction(sender: UITextField) {
	if sender === usernameField {
		sender.resignFirstResponder()
		passwordField.becomeFirstResponder()
	}
}

I don't seem to need to call setNeedsLayout().

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3237732View Question on Stackoverflow
Solution 1 - IosDave BattonView Answer on Stackoverflow
Solution 2 - IosliamnicholsView Answer on Stackoverflow
Solution 3 - IosIT GypsyView Answer on Stackoverflow
Solution 4 - IosHimanshu MahajanView Answer on Stackoverflow
Solution 5 - IosRamakrishnaView Answer on Stackoverflow
Solution 6 - Iosd.lebedevView Answer on Stackoverflow
Solution 7 - IosSanthosh RView Answer on Stackoverflow
Solution 8 - IosDaniel T.View Answer on Stackoverflow