UITextField Text Transitions

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

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

This logic should switch the user from the text input field to the password when he clicks the "Next" button on the keyboard. But I'm stuck with a glitch: after

 self.passwordField.becomeFirstResponder() 

the text in the input field moves to the upper left corner and back. And what is more strange: this glitch is reproduced only for the first time, then you need to recreate the ViewController to observe this behavior

Here is a video with a 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 } 
+45
ios uitextfield swift
Sep 24 '15 at 15:32
source share
8 answers

Based on some other ideas posted here, this solution, which is easy to implement, works (for me) in all cases and has no side effects:

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

This solution also works if you programmatically move to the next field from -textFieldShouldReturn: or if the user simply touches another responder.

+47
Oct. 25 '15 at 20:12
source share

In a subclass of UITextField you can do the following:

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

The trick here is for you to call layoutIfNeeded after the layoutIfNeeded been called.

The implementation of this method is very convenient because you do not need to call resignFirstResponder in the delegate callbacks, as this caused me problems inside the UIScrollView , however it wasnโ€™t higher :)

+13
Nov 03 '15 at 15:31
source share

Use this code to avoid skipping text in a 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]; } 
+8
Jan 08 '16 at 11:09
source share

I am also facing the same problem. The following code works for me.

 func textFieldDidEndEditing(_ textField: UITextField) { textField.layoutIfNeeded() } 
+5
Mar 16 '17 at 9:06
source share
 func textFieldDidEndEditing(_ textField: UITextField) { textField.layoutIfNeeded() } 
+3
Mar 03 '17 at 11:02
source share

Based on what I understand from this :

This problem can be caused when you have layout changes, or animations processed in keyboard callbacks show and hide notifications (usually in cases when it is required that the text field be pressed so that the keyboard does not hide it).

Solution: I ran into this problem, since I did layoutIfNeeded every time the keyboard was displayed, received a call, considering it safe, obviously it is not there, so when I check to do this, only when there is a need to change frames, the transition has stopped.

+1
Apr 11 '17 at 18:02 on
source share

I do not pass text fields to delegates. Instead, I create an IBAction and attach it to the Finished Exit event. A crash also happens with this method, but only in iOS 9. It looks like an OS error.

My actions are as follows:

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

The above crashes, but when I do below, the glitch goes away:

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

I do not need to call setNeedsLayout() .

0
Oct. 15 '15 at 20:19
source share

A more โ€œgeneralโ€ 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

0
Oct 22 '15 at 12:16
source share



All Articles