8

After switching to iOS8, I'm getting weird behavior when I move views during a keyboard transition. Can anyone explain what's going on?

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

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

When I tap the text field, keyboardWillShow calls nudgeUp, but the behaviour is very different. The button and text field immediately jump down by 10 points, and then slide back up to their original position as the keyboard shows itself.

Why is this happening? How can I regain control of animations 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
Pitarou
  • 2,211
  • 18
  • 25
  • I found this question because I'm seeing that the UIKeyboardWillShowNotification is never even being posted. (As in, a breakpoint within the selector referenced in the addObserver call is never hit!). Did you verify that the notification is actually being posted to you? (I ask because although learning that iOS 8 and auto layout may be interfering, my app is old enough to not have been built with auto layout in place). – tobinjim Oct 21 '14 at 22:44
  • Update: Created a new single-window app with a textfield in it and the notification posts regardless of AutoLayout checkbox... hmm) – tobinjim Oct 21 '14 at 23:32
  • Sorry, can't help you. The notification is certainly being posted in my code. – Pitarou Oct 22 '14 at 09:07

3 Answers3

8

It's AutoLayout. Something changed in iOS8 and you can't just change frame or center points anymore if you have AutoLayout enabled. You have to create an outlet(s) of your constraint (vertical space) and update it accordingly instead of changing frame position. Constraints are like any other ui control and can have an outlet. Constraint change 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) {
}];
Haris
  • 96
  • 4
  • Many thanks for your insightful answer. I placed a 200 point bounty on this question. I wish I could have awarded it to you, but by the time you answered the bounty had already expired. – Pitarou Oct 16 '14 at 02:43
  • This is really tricky. I have spent days on this problem, thanks for the info. – Dino Tw Sep 30 '15 at 06:01
0

You should use UIKeyboardDidShowNotification (you're using 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 are changing the frames too early. After your changes the system will relayout everything to accomodate the keyboard and your changes won't have any effect.

Also, I recommend you to switch to autolayout and forget about frames.

djromero
  • 19,551
  • 4
  • 71
  • 68
  • Your solution does what you say it will, but I don't want my animation to happen *after* the keyboard has finished appearing; I don't understand how the behavior I see could be described as a "relayout to accommodate the keyboard"; and I don't understand why my code worked in iOS7 but not in iOS8. – Pitarou Oct 06 '14 at 12:48
0

Try using the UIKeyboardWillShowNotification userInfo to give you the frame of the keyboard. Then move the onscreen elements based on that.

CaptainCOOLGUY
  • 1,131
  • 1
  • 14
  • 26
  • That's what I've been doing all along, and it works great in iOS7. But in iOS8 it behaves in the same way as the (much simpler) example code I gave above: the frame jumps in the *opposite* direction from the way I tell it to, and then slides back to its original position. – Pitarou Oct 09 '14 at 02:19