我正在做一个消息应用程序,我正试图用UITextField和两个按钮(相机和完成)复制键盘顶部的工具栏。当你点击视图中的UITextField时,我知道如何将工具栏放在键盘上,但我的文本字段在我的工具栏上。当点击工具栏上的UITextField时,我希望键盘出现在它下面,并将工具栏向上推。我还需要它们一起向下,就像它们是一回事一样(就像在iMessage应用程序中一样)。我查阅了许多教程,但似乎找不到答案。我需要什么代码才能执行此操作??谢谢,我不是在问如何将工具栏放在键盘上,我是在问如何触发键盘,使其位于现有工具栏下,并被所述工具栏内的文本视图触发
我写的这段代码复制了iMessage的效果,它使用了一个开源的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;
}