I am doing a messaging app and I am trying to duplicate the toolbar on top of the key board with the UITextField and two buttons (camera and done). I know how to put a toolbar on top of the keyboard when you click on a UITextField in the view, but my textfield is on my toolbar. When the UITextField on the toolbar is clicked, I want the keyboard to come up under it and kind of push the toolbar up with it. I also need them to go down together as if they are one thing (like it does in the iMessage app). I have looked up many tutorials but cannot seem to find the answer. What code do I need to do this?? Thank you I AM NOT ASKING HOW TO PUT A TOOLBAR ON TOP OF A KEYBOARD, I AM ASKING HOW TO TRIGGER THE KEYBOARD TO COME UP UNDER THE EXISTING TOOLBAR AND BE TRIGGERED BY THE TEXTVIEW INSIDE SAID TOOLBAR
Asked
Active
Viewed 1,054 times
1 Answers
0
i have written this code duplicate the effect of iMessage, it uses a open source textview HPGrowingTextView
- (void)organizeView {
textView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(8, 5, 230, 40)];
textView.contentInset = UIEdgeInsetsMake(0, 5, 0, 5);
textView.minNumberOfLines = 1;
textView.maxNumberOfLines = 4;
// you can also set the maximum height in points with maxHeight
// textView.maxHeight = 200.0f;
textView.returnKeyType = UIReturnKeySend; //just as an example
textView.delegate=self;
textView.font = [UIFont systemFontOfSize:15.0f];
textView.delegate = self;
textView.internalTextView.scrollIndicatorInsets = UIEdgeInsetsMake(5, 0, 5, 0);
textView.backgroundColor = [UIColor whiteColor];
textView.placeholder = @"Type your Comment here..";
[textView becomeFirstResponder];
UIImage *rawEntryBackground = [UIImage imageNamed:@""];
UIImage *entryBackground = [rawEntryBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22];
UIImageView *entryImageView = [[UIImageView alloc] initWithImage:entryBackground];
entryImageView.frame = CGRectMake(5, 0, 248, 40);
entryImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UIImage *rawBackground = [UIImage imageNamed:@"message-strip.png"];
UIImage *background = [rawBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22];
UIImageView *imageView = [[UIImageView alloc] initWithImage:background];
imageView.frame = CGRectMake(0, 0, _containerView.frame.size.width, _containerView.frame.size.height);
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// view hierachy
[_containerView addSubview:imageView];
[_containerView addSubview:textView];
[_containerView addSubview:entryImageView];
UIImage *sendBtnBackground = [[UIImage imageNamed:@"send"] stretchableImageWithLeftCapWidth:13 topCapHeight:0];
UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
doneBtn.frame = CGRectMake(_containerView.frame.size.width - 69, 8, 63, 27);
doneBtn.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
[doneBtn setImage:sendBtnBackground forState:UIControlStateNormal];
[doneBtn addTarget:self action:@selector(postButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[_containerView addSubview:doneBtn];
_containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
}
-(void)resignTextView
{
[textView resignFirstResponder];
}
//Code from Brett Schumann
-(void) keyboardWillShow:(NSNotification *)note{
// get keyboard size and loctaion
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// Need to translate the bounds to account for rotation.
keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
NSInteger kbSizeH = keyboardBounds.size.height;
// get a rect for the textView frame
CGRect containerFrame = _containerView.frame;
containerFrame.origin.y -= kbSizeH-50;
////reset the table container view height
CGRect tableContainerFrame=_viewTable.frame;
tableContainerFrame.size.height=self.view.frame.size.height-(kbSizeH+containerFrame.size.height );
//containerFrame.origin.y = self.view.bounds.size.height - (keyboardBounds.size.height + containerFrame.size.height);
// animations settings
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
// set views with new info
_containerView.frame = containerFrame;
_viewTable.frame=tableContainerFrame;
// commit animations
[UIView commitAnimations];
}
-(void) keyboardWillHide:(NSNotification *)note{
NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// get a rect for the textView frame
CGRect containerFrame = _containerView.frame;
containerFrame.origin.y = self.view.bounds.size.height - containerFrame.size.height;
// animations settings
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
// set views with new info
_containerView.frame = containerFrame;
// commit animations
[UIView commitAnimations];
}
- (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height
{
float diff = (growingTextView.frame.size.height - height);
CGRect r = _containerView.frame;
r.size.height -= diff;
r.origin.y += diff;
_containerView.frame = r;
}
-(BOOL)growingTextViewShouldReturn:(HPGrowingTextView *)growingTextView{
if (growingTextView == self->textView) {
[self performSelector:@selector(postButtonAction:)];
return NO;
}
else
return YES;
}

Pawan Rai
- 3,434
- 4
- 32
- 42
-
I apologize in advanced for my ignorance. I copied and pasted this code in my project and then made the HPGrowingTextView property but I still have 20 errors. What else do I have to do to make this code work in my project. Thank you very much – Cooper Edmunds Feb 15 '14 at 19:00
-
most of my errors are "Use of undeclared identifier 'textView'" and "Use of undeclared identifier 'containerView'" and then the other two are "Expected a type" with HPGrowingTextView in red. thanks again – Cooper Edmunds Feb 15 '14 at 19:03
-
define HPGrowingTextView *textView; in .h – Pawan Rai Feb 15 '14 at 19:18
-
i have posted this code for basic idea behined iMessage chat box. its a little bit old code. for easy implementation you can try any of this component, follow this [link](https://www.cocoacontrols.com/search?utf8=%E2%9C%93&q=chat) – Pawan Rai Feb 15 '14 at 19:20
-
So in the .h I say "@property (nonatomic, retain) IBOutlet HPGrowingTextField *textView? Because when I do that I have different errors. But still 20 – Cooper Edmunds Feb 15 '14 at 19:31
-
dont forget to @synthesize textView; – Pawan Rai Feb 15 '14 at 19:33
-
I mean HPGrowingTextView – Cooper Edmunds Feb 15 '14 at 19:34
-
Yeah I didn't. I synthesized it after I put the property in the .h under the interface. It says "Unknown type name 'HPGrowingTextView'" – Cooper Edmunds Feb 15 '14 at 19:39
-
have you added this class 'HPGrowingTextView' to your project ? & imported it to class #import "HPGrowingTextView.h" – Pawan Rai Feb 15 '14 at 19:43
-
check this link for [HPGrowingTextView](https://github.com/HansPinckaers/GrowingTextView) – Pawan Rai Feb 15 '14 at 19:44
-
No I was just putting all of this code in an existing view controller but there was nothing in it. What is the subclass of"HPGrowingTextView"??? I am very confused. Am I supposed to add all of this code into the HPGrowingTextView VC???? – Cooper Edmunds Feb 15 '14 at 19:54
-
no its a textView, better will be . just post your code to dropbox or somewhere and put a link i will fix it. – Pawan Rai Feb 15 '14 at 20:00
-
do you want me to post my whole project (which is pretty small) or just the code that you gave me? – Cooper Edmunds Feb 15 '14 at 20:25
-
1the whole project code . so i can fix it and return it back to you without error. – Pawan Rai Feb 15 '14 at 20:28
-
dropbox isn't going to work. Where should I put it? – Cooper Edmunds Feb 15 '14 at 20:37
-
do you want me to email it to you? – Cooper Edmunds Feb 15 '14 at 20:37
-
mail it to me , ghz.rai@gmail.com – Pawan Rai Feb 16 '14 at 05:24