views:

51

answers:

0

I have a UIViewController embedding a UITextField. When the user starts editing the text field, the view of the view controller is descreased in size so that the keyboard won't cover up the textfield:

- (void)keyboardWillShow:(NSNotification *)note {
// Find the height of the keyboard
CGRect keyboardRect;
[[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue:&keyboardRect];
CGFloat keyboardHeight = keyboardRect.size.height;
CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;

CGRect tableFrame = self.tableView.frame;
tableFrame.size.height -= (keyboardHeight - tabBarHeight);

self.tableView.frame = tableFrame;  // <-- This line causes the problem
}

- (void)keyboardWillHide:(NSNotification *)note {
// Find the height of the keyboard
CGRect keyboardRect;
[[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue:&keyboardRect];
CGFloat keyboardHeight = keyboardRect.size.height;
CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;

CGRect tableFrame = self.tableView.frame;
tableFrame.size.height += (keyboardHeight - tabBarHeight);

self.tableView.frame = tableFrame; // <-- This line causes the problem
}

The above works fine, until the view is rotated to landscape mode. The keyboard is first removed, then slides in diagonally from the bottom left. At this point it is completely unresponsive. Turning the device back to portrait creates another keyboard on top of the first one.

I have tried removing the line marked in the code above ("This line causes the problem") and this removes the problem. However, this makes the keyboard cover up the textfield, and is therefore not a good solution.

Any ideas as to how this can be fixed?

Edit: Upon further inspection, I found that the two methods above are called twice each when the view is rotating, in the following order:

  • keyboardWillShow
  • keyboardWillHide
  • keyboardWillShow
  • keyboardWillHide

In my other, similar viewcontrollers the methods are only called once each. Could this be the cause?