0

I have a problem with [UIView animationWithDuration] when trying to animate a button when the keyboard come up. I have a notification tell me when the keyboard comes up then I have the ViewController call this method in the view. The button animates but not correctly. It just animates from a random position on the screen to the position it was at previously. The reason I bring this up again is because I've seen answers from other posts saying that setting the view's translatesAutoresizingMaskIntoConstraints property to YES in the ViewController will fix the problem, but it doesn't fix the problem.

    [self.view setTranslatesAutoresizingMaskIntoConstraints:YES];

The animation is below:

- (void)animateForKeyboardAppearanceWithKeyboardSize:(CGSize)keyboardSize; {
    [UIView animateWithDuration:0.3f
                 animations:^{
                     [self.sendSMSButton setFrame:CGRectMake(0,
                                                             self.frame.size.height - keyboardSize.height - self.sendSMSButton.frame.size.height,
                                                             self.frame.size.width,
                                                             self.sendSMSButton.frame.size.height)];
                 }];
}

Anyone know what's happening? Thanks in advance.

user1072264
  • 121
  • 2
  • 11
  • Do you use auto layout? If so, you better set `NSLayoutConstraint`'s `constant` property in animation block. Then invoke `updateConstraintsIfNeeded`. – Ryan Jan 30 '15 at 02:47
  • If you are not using `Autoloayout`, there's no reason to use `setTranslatesAutoresizingMaskIntoConstraints:`. Where do you call the `animateForKeyboardAppearanceWithKeyboardSize:`? – Ryan Jan 30 '15 at 02:54
  • I'm not using nibs, so I don't think that applies here. Thanks for the response though. – user1072264 Jan 30 '15 at 02:54
  • I call `animateForKeyboardAppearanceWithKeyboardSize:` in the viewcontroller when it gets a notification that the keyboard is about to appear. `- (void)keyboardWillShow:(NSNotification *)notification { NSDictionary *userInfo = [notification userInfo]; CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; [self.createAccountView animateForKeyboardAppearanceWithKeyboardSize:keyboardSize]; }` – user1072264 Jan 30 '15 at 02:57
  • Once you've figured this out, you might want to look at some ideas found here to improve your animation. http://stackoverflow.com/questions/18837166/how-to-mimic-keyboard-animation-on-ios-7-to-add-done-button-to-numeric-keyboar – Ricky Jan 30 '15 at 03:04
  • @user1072264 Are you sure that the notification is posted and the method is invoked? – Ryan Jan 30 '15 at 03:09
  • Yes, the notification is posted and the method is invoked. – user1072264 Jan 30 '15 at 03:29

1 Answers1

0

Have you ensured that this method isn't being called repeatedly? Also try subclassing the button, override -setFrame and double check to see if something else is setting the frame of your button while you are trying to animate it.

Ricky
  • 3,101
  • 1
  • 25
  • 33
  • The function is only being called once. The fact of the matter is that anytime I set the frame (inside or outside the animation block), the button animates in the same manner (incorrectly). This also happens when resizing all other views. – user1072264 Jan 30 '15 at 03:09
  • Are there any noticeably interesting things about the button, like a transform? Maybe try replace it with a simple coloured UIView. – Ricky Jan 30 '15 at 03:11
  • The button is all white against a green background. Even when I set its frame to CGRectMake(0,0,100,100); (even outside an animation block), the button will stay in the same position and won't do what it's told. – user1072264 Jan 30 '15 at 03:30
  • Have you altered the layer of the button, or anything similar of its superview? I think you're going to have to start looking beyond the individual button to find your answer. – Ricky Jan 30 '15 at 03:31