Animated preview of UIKeyboardWillShowNotification event in iOS 8

I am using the following code to animate the view position according to keyboard movement. The code works fine under iOS 7, however it causes strange behavior on iOS 8:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

- (void) keyboardWillShow:(NSNotification*) aNotification {  
    NSDictionary* keyboardInfo = [aNotification userInfo];
    NSTimeInterval time = [keyboardInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] intValue];
    CGRect keyboardFrameEnd = [[keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    double offset = keyboardFrameEnd.size.width > keyboardFrameEnd.size.height ? keyboardFrameEnd.size.height : keyboardFrameEnd.size.width;

    [UIView beginAnimations:@"moveWindow" context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationCurve:curve];
    [UIView setAnimationDuration:time];

    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y + offset, self.frame.size.width, self.frame.size.height);

    [UIView commitAnimations];
}

      

Sorry, the answers mentioned in Are there changes to the behavior of UIKeyboardWillShowNotification in iOS 8? and iOS8: What about moving views during keyboard transitions? out of the question:

  • The source code base is very large and old and does not use storyboards or auto-layout features.
  • I don't want to use the event UIKeyboardDidShowNotification

    , because the keyboard is already visible and the animation looks terrible (however, the problem went away when using this type of notification).

Oddly enough, I found out that the problem went away when this feature was used on the first screen of the app. Further tests showed that removing MKMapView

from one of the previous screens solves this problem. I have triple checked what MKMapView

is being used and placed correctly. Each allocated instance is gone before the above code is executed.

After hours of testing and debugging, I noticed in the visual hierarchy debugger ( how to check the view hierarchy in iOS? ) That there are limitations added to UILayoutContainerView

and UINavigationTransitionView

. These restrictions do not exist if the control is MKMapView

removed from the previous screen. I've tried playing with every possible setTranslatesAutoresizingMaskIntoConstraints:

setup combination and haven't found any resolution yet. Is this some kind of bug in iOS 8 itself, or is there some other way to liven up the view along with the keyboard?

+3


source to share


1 answer


I'm not sure about the code itself, but from the documentation about these animation methods ...

Using this method is deprecated in iOS 4.0 and later. You should use block based animation techniques to specify your animations.

The correct way to animate the view is ...



[UIView animateWithDuration:time
                      delay:0
                    options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{
                     self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y + offset, self.frame.size.width, self.frame.size.height);
                 }
                 completion:nil];

      

Edit

Ok, this is not AutoLayout (I think), but I'll leave that here as you need to change it and I'll wait for more information in the question.

0


source







All Articles