我知道如何使用UITextField
显示键盘配件。
textField.inputAccessoryView = keyboardToolBar;
但是,如果我没有UITextField
而是按钮,该怎么办?如何显示具有工具栏的键盘?
工具栏本身将包含UITextField
,我必须以编程方式首先响应。
但这怎么会有用?
更新
-(void)createKeyboardToolBar
{
self.keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 42)];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(8, 6, 250, 30)];
UIBarButtonItem *textBtn = [[UIBarButtonItem alloc] initWithCustomView:self.textField ];
UIBarButtonItem *postBtn = [[UIBarButtonItem alloc]initWithTitle:@"Post" style:UIBarButtonItemStyleBordered target:self action:@selector(postComment)];
[self.keyboardToolBar setItems: [NSArray arrayWithObjects:textBtn,postBtn,nil]];
self.textField.inputAccessoryView = self.keyboardToolBar;
}
所以按照这些建议,我想出了上面的代码。但是,当我单击应该使self.textField
作为第一个响应者的按钮时,会进行调用,但键盘或工具栏不会出现。 (我使用NSLog)
答案 0 :(得分:0)
为您的按钮添加操作 buttonClicked 。因此,当您使用成为第一个响应者为textfield
点击按钮显示键盘时。
- (void)buttonClicked {
[myTextField becomeFirstResponder];
}
答案 1 :(得分:0)
这是解决方案
- (IBAction)keyboardaction:(id)sender {
self.keyToolbar_.hidden=NO;
CGRect lframe = self.keyToolbar_.frame;
lframe.origin.y = [[UIScreen mainScreen] bounds].size.height-<keyboardheight>;
[self.entryTxfld_ becomeFirstResponder];
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^ {
self.keyToolbar_.frame = lframe;
}completion:^(BOOL finished) {
}];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
CGRect lframe = self.keyToolbar_.frame;
lframe.origin.y = [[UIScreen mainScreen] bounds].size.height;
[UIView animateWithDuration:0.6
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^ {
self.keyToolbar_.frame = lframe;
}completion:^(BOOL finished) {
}];
[textField resignFirstResponder];
return YES;
}
在XIB for View中添加工具栏,底部和工具栏中添加了textfield。供参考查找图像。