Search code examples
iosuiviewios7uikitcore-animation

UIAlertView and block based animations


I have animation code that shifts a button and some other views in response to a keyboard showing up:

NSValue *frameValue = [note userInfo][UIKeyboardFrameEndUserInfoKey];
CGRect keyboardFrame = [frameValue CGRectValue];
CGFloat keyboardHeight = keyboardFrame.size.height*kCGPKeyboardAnimationTranslationAdjustmentFactor;
CGFloat animationDuration = [[note userInfo][UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationOptions animationCurveOption = [[note userInfo][UIKeyboardAnimationCurveUserInfoKey] integerValue] >> 16;

CGPoint submitButtonCenter = [[self submitButton] center];
CGPoint offsetSubmitButtonCenter = CGPointMake(submitButtonCenter.x, submitButtonCenter.y-keyboardHeight);

__block __weak __typeof(self) wSelf = self;
NSCParameterAssert([wSelf isKindOfClass:[self class]]);
[UIView animateWithDuration:animationDuration delay:0.0f options:animationCurveOption animations:^{

    [[wSelf submitButton] setCenter:offsetSubmitButtonCenter];

} completion:nil
    }];

However this animation gets undone later, when I display a UIAlertView

- (void)displayError:(NSError*)error {
    NSCParameterAssert([NSThread isMainThread]);
    NSString *errorDescription = [error description];
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"" message:errorDescription delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    NSLog(@"%s: Recursive View Description: %@",__PRETTY_FUNCTION__,[[[self view] subviews] description]);
    [av show];
    NSLog(@"Is it still there? %s Recursive View Description: %@",__PRETTY_FUNCTION__,[[[self view] subviews] description]);
}

The buttons frame gets changed right after [av show] is called, reverting it back to the exact position it was in prior to the animation. Commenting out [av show] makes the bug go away, so it looks like a side effect.

Though that code isn't there, there are several other UIView and UITextField objects that get shifted the same way. They are unaffected, only the UIButton moves.

Anyone have ideas on how to fix it?


Solution

  • After a few hours of debugging and figuring out the root cause, the answer turned out to be straightforward.

    Dismissing the UIAlertView triggered Auto Layout, which is what moved the Submit Button back to where it was before. Setting a contraint or turning auto layout off and using autoresizing masks fixed the issue.