IOS8: What happens to moving views when switching to the keyboard?

After switching to iOS8, I get weird behavior when I move views while switching to the keyboard. Can someone explain what is happening?

Here is a minimal example to demonstrate the problem. I have a simple view with a UITextField and a UIButton . The nudgeUp function moves the text box, and the button moves it 10 points. It is triggered either by the buttonPressed callback or the keyboardWillShow callback.

When I click the button, the code works as expected: buttonPressed calls nudgeUp , and the button and text field jump up 10 points.

When I click on the text box, keyboardWillShow calls nudgeUp , but the behavior is very different. The button and the text box immediately jump 10 points, and then return to their original position when the keyboard shows itself.

Why is this happening? How to restore animation control during keyboard presentation in iOS8?

 #import "ViewController.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; } - (void)keyboardWillShow:(NSNotification *)notification { // Called when the keyboard appears. [self nudgeUp]; } - (IBAction)buttonPressed:(id)sender { [self nudgeUp]; } - (void)nudgeUp { CGRect newTextFieldFrame = self.textField.frame; newTextFieldFrame.origin.y -= 10; self.textField.frame = newTextFieldFrame; CGRect newButtonFrame = self.button.frame; newButtonFrame.origin.y -= 10; self.button.frame = newButtonFrame; } @end 
+8
uikeyboard animation ios8
source share
3 answers

This is AutoLayout. Something has changed in iOS8, and you can no longer change frames or center points if AutoLayout is turned on. You must create the output of your constraint (vertical space) and update it accordingly instead of changing the position of the frame. Constraints are similar to any other ui control and may have an output. Changing restrictions can be animated.

Example:

 [UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] delay:0 options:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] animations:^{ self.bottomSpaceConstraint.constant = adjustmentedValue; [self.view layoutIfNeeded]; } completion:^(BOOL finished) { }]; 
+8
source share

You should use UIKeyboardDidShowNotification (you are using the will version), and everything will work as you expect:

 - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; } - (void)keyboardDidShow:(NSNotification *)notification { // Called when the keyboard finished showing up [self nudgeUp]; } 

The explanation is that with UIKeyboardWillShowNotification you change the frame too soon. After your changes, the system will relay everything to accommodate the keyboard, and your changes will have no effect.

In addition, I recommend that you switch to autorun and forget about frames.

0
source share

Try using custom UIKeyboardWillShowNotification information to provide you with a keyboard frame. Then move the screen elements based on this.

0
source share

All Articles