It has been a while since this was set, but for the sake of documentation it says: First, sign up for NSNotificationCenter to receive a notification about the keyboard:
-(void) viewWillAppear:(BOOL)animated { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillToggle:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillToggle:) name:UIKeyboardWillHideNotification object:nil]; }
don't forget to unsubscribe
- (void)viewWillDisappear:(BOOL)animated { [self.view endEditing:YES]; [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; }
Then we implement the function that will be called by the notification center:
- (void) keyboardWillToggle:(NSNotification *)aNotification { CGRect frame = [[[self tabBarController] tabBar] frame]; CGRect keyboard = [[aNotification.userInfo valueForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue]; frame.origin.y = keyboard.origin.y - frame.size.height; [UIView animateWithDuration:[[aNotification.userInfo valueForKey:@"UIKeyboardAnimationDurationUserInfoKey"] floatValue] animations:^ { [[[self tabBarController] tabBar] setFrame:frame]; }];
This will animate the TabBar with a keyboard step and hold it on top.
Agustin
source share