0

This code was working beautifully in ios 7. However, with ios8 and xcode 6.0.1 it has stopped working. When a user clicked on a text field to enter text, the field animated to float just above the top of the keyboard so they can see what they are typing. Any thoughts on why this fails to work now. I can see it start to animate for a split second, but then the textfield disappears. Frustrating.

- (void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.4];
[UIView setAnimationBeginsFromCurrentState:YES];
textField.frame = CGRectMake(textField.frame.origin.x, (textField.frame.origin.y - 200.0),            
textField.frame.size.width, textField.frame.size.height);

_searchBtn.frame = CGRectMake(_searchBtn.frame.origin.x, (_searchBtn.frame.origin.y - 200.0), 
_searchBtn.frame.size.width, _searchBtn.frame.size.height);
[UIView commitAnimations];


textField.alpha = .75;


}
hansoac
  • 47
  • 8
  • 1
    Seems like you are having the same problem as the following guy http://stackoverflow.com/questions/26045048/how-to-implement-newsfeed-comment-page-similar-to-facebook-or-instagram. Maybe his question has some answers for you. If his question includes yours, you should upvote it to bring more attention to it. But my guess is his question will help you a bit. For example your 200 is hardcoded. But instead you should be watching the keyboard notification so you could extract the height. For a test, try changing the number to 250 and see what happens. – Konsol Labapen Sep 25 '14 at 18:55
  • Thanks for the reply, Konsol. I tried adapting the code to the actual height of the keyboard dynamically, still no luck. I also tried playing with these numbers. Thank you very much for your suggestions so far. Someone has to have an answer for this! – hansoac Sep 25 '14 at 19:09

1 Answers1

1

The problem is that your code adjusts your TextField's position by a fixed value of 200.0

This was probably great for iOS7 but things have changed in iOS8 for two reasons:

  • The system keyboard has an additional view for showing predicted words while typing
  • Custom keyboards can be as high as the developer chooses

You need to change your approach move moving the location of your TextField whenever the keyboard is shown or hidden using the following two notifications:

  • UIKeyboardWillShowNotification
  • UIKeyboardWillHideNotification

In the following thread I explain the problems that can arise and how to properly move your views around as the keyboard opens:

can't get correct value of keyboard height in iOS8

EDIT 1: Assuming the textbox has an outlet called "yourTextBox", the code to modify the position of your textbox could look something like:

CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
CGFloat keyboardHeight = <<calculated keyboard height as previously discussed>>;
CGFloat textBoxHeight = _yourTextBox.frame.size.height;

// Change the current frame of your textbox in order to reposition it
CGFrame textBoxFrame = _yourTextBox.frame;
textBoxFrame.origin.y = screenHeight - keyboardHeight - textBoxHeight;
_yourTextBox.frame = textBoxFrame;

Note: If you are using AutoLayout constraints to position your subviews you will need to avoid modifying the frame and change your constraints instead. Post if you are having problems in this area because it can get tricky.

Community
  • 1
  • 1
dgangsta
  • 1,315
  • 2
  • 7
  • 4
  • This is great thank you! I have read through your post and am implementing your suggestions. However what would my code look like to move the textbox just above the current keyboard height? Is it still the GCRectMake? – hansoac Sep 26 '14 at 01:56