I have a UIToolbar at the bottom of my ViewController that is inherited from the self.navigationController
. I show it in my ViewController in the viewWillAppear:
method using this code:
[self.navigationController setToolbarHidden:NO animated:NO];
self.navigationController.toolbar.backgroundColor = [UIColor blackColor];
self.navigationController.toolbar.tintColor = [UIColor blackColor];
self.navigationController.toolbar.barStyle = UIBarStyleBlackOpaque;
After that, my ViewController has both programmatically added an UITextView and an UIButton that will allow the user to add comments to the ViewController, which is in fact, a UITableViewController. I would like the entire UIToolbar to move up to the very top of the keyboard whenever my UITextView has begun editing.
To accomplish this, I have A)
-Added the following observer's to my ViewController in the viewDidLoad:
method
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
And B)
-Added these following methods to control the repositioning of the navigationController's inherent UIToolbar .
- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSDictionary* keyboardInfo = [aNotification userInfo];
NSValue* keyboardFrameEnd = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardFrameEndRect = [keyboardFrameEnd CGRectValue];
// the keyboard is showing so resize the table's height
NSTimeInterval animationDuration =
[[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame; //self.view.frame
self.navigationController.toolbar = CGRectMake(self.navigationController.toolbar.frame.origin.x,
self.navigationController.toolbar.frame.origin.y + 10,
self.navigationController.toolbar.frame.size.width,
self.navigationController.toolbar.frame.size.height);
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
NSDictionary* keyboardInfo = [aNotification userInfo];
NSValue* keyboardFrameEnd = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardFrameEndRect = [keyboardFrameEnd CGRectValue];
// the keyboard is hiding reset the table's height
NSTimeInterval animationDuration =
[[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.origin.y += self.navigationController.toolbar.frame.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
}
I've have tried all sorts of methods for trying to get this to work and I am finding this methodology to be the simplest for my application. I'm getting an error:
Assignment to read-only property
on this line:
self.navigationController.toolbar = CGRectMake(self.navigationController.toolbar.frame.origin.x,
self.navigationController.toolbar.frame.origin.y + 10,
self.navigationController.toolbar.frame.size.width,
self.navigationController.toolbar.frame.size.height);
in the keyboardWillShow:
method. It appears that I cannot change the position of the navigationController's inherent toolbar, does this mean that I will need to create my UIToolbar programmatically?
Also, if possible, how can I simply make this code achieve my goal without complicating things and making a programmatic toolbar, I'd rather stick with the inherent toolbar. Thanks.
First:
self.view do not have the toolbar, you have to move your toolbar separately..
Second:
this is wrong,
self.navigationController.toolbar.frame = CGRectMake..
is the correct way.Third:
Using
self.navigationController.toolbar.frame.origin.y - keyboardFrame.size.height
i think you have to correctly calculate the frame of theself.navigationController.toolbar
based from the height of the keyboard, and animate it..And another option is using UITextFields
-inputAccessoryView
(if you're working with UITextField) if you have your own toolbar(made programmatically/customed)..