在ios上单击按钮显示带有工具栏的键盘

时间:2014-09-25 06:30:17

标签: ios keyboard uitoolbar

我知道如何使用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)

2 个答案:

答案 0 :(得分:0)

为您的按钮添加操作 buttonClicked 。因此,当您使用成为第一个响应者textfield点击按钮显示键盘时。

- (void)buttonClicked {

[myTextField becomeFirstResponder];

}

答案 1 :(得分:0)

  1. 全局分配文本字段,工具栏,然后将其添加到工具栏中。
  2. 在按钮操作中,为文本字段写下“成为第一响应者”。
  3. 这是解决方案

    - (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;
    }
    

    enter image description here

    在XIB for View中添加工具栏,底部和工具栏中添加了textfield。供参考查找图像。 enter image description here