【问题标题】:Add a done button to the top of the keyboard在键盘顶部添加完成按钮
【发布时间】:2014-03-05 07:09:47
【问题描述】:

我正在制作一个具有 UITextView 的通用应用程序。 当应用程序在 iPad 上运行时,右下角有一个按钮,可以让我关闭键盘。 iPhone版本没有这样的按钮。 我在一些 iPhone 应用程序上看到了键盘顶部的一个栏,它有一个完成选项。 是否有一种简单的方法可以将 iPad 风格的关闭键盘按钮也添加到 iPhone 应用程序中。 如果没有,将完成样式栏添加到键盘顶部的最佳方法是什么? 提前致谢。

【问题讨论】:

    标签: ios iphone keyboard


    【解决方案1】:

    请试试这个代码

     //set up a placeholder variable for the textfield user typing
     UITextView *currentTextView;
    
    -(void)addDoneToolBarToKeyboard:(UITextView *)textView
    {
        UIToolbar* doneToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
        doneToolbar.barStyle = UIBarStyleBlackTranslucent;
        doneToolbar.items = [NSArray arrayWithObjects:
                           [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                           [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonClickedDismissKeyboard)],
                           nil];
        [doneToolbar sizeToFit];
        textView.inputAccessoryView = doneToolbar;
    }
    
     //remember to set your text view delegate
     //but if you only have 1 text view in your view controller
     //you can simply change currentTextField to the name of your text view
     //and ignore this textViewDidBeginEditing delegate method
     - (void)textViewDidBeginEditing:(UITextView *)textView
     {
         currentTextView = textView;
     }
    
    -(void)doneButtonClickedDismissKeyboard
    {
         [currentTextView resignFirstResponder];
    }
    

    在你的视图中添加这个确实加载了

     [self addDoneToolBarToKeyboard:self.textView];
    

    希望有帮助

    【讨论】:

    • 并且还实现了doneWithNumberPad: 方法,在文本字段上实际辞职第一响应者。
    • @rmaddy 我意识到,实际上是在编辑......因为在我的应用程序中它是一个数字键盘,在这种情况下,我将该名称更改为一个稍微通用的名称
    • 这也适用于我的 UITextView 吗?我看到代码引用了 UITextField?
    • @BennyAbramovici 是的,那会,让我编辑我的答案,你可以试试。
    • 这很好用,我正在我自己的应用程序中使用它。我建议更改色调颜色,因为默认的蓝色可能不是与黑色半透明条的最佳匹配。我刚刚在设置 inputAccessoryView 之前添加了[doneToolbar setTintColour:[UIColor whiteColor]];
    【解决方案2】:

    同样的答案swift3

    func addDoneToolBarToKeyboard(textView:UITextView)
    {
        let doneToolbar : UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 50))
        doneToolbar.barStyle = UIBarStyle.default
        let flexibelSpaceItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
    
        let hideKeyboardItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(self.dismissKeyboard))
        doneToolbar.items = [flexibelSpaceItem, hideKeyboardItem!]
        doneToolbar.sizeToFit()
        textView.inputAccessoryView = doneToolbar
    }
    

    和解除功能将是:

    func dismissKeyboard()
    {
      self.view.endEditing(true)
    }
    

    【讨论】:

      【解决方案3】:

      您可以随时在一行代码中执行此操作:

      [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) 
                                                 to:nil 
                                               from:nil
                                           forEvent:nil];
      

      这会将resignFirstResponder 发送到响应者链。当前文本视图将是第一响应者,因此它将收到消息并退出第一响应者。

      【讨论】:

      • 谢谢汤姆,这两种方法都可以收回键盘。问题是当点击完成按钮时,它只会缩回 iPhone 版本的键盘,而不是 iPad。在 iPad 上它什么也不做,尽管 ipad 键盘无论如何都可以用缩回按钮缩回
      【解决方案4】:

      我知道这是一个为时已晚的答案,但在 iOS 8 中,我无法使用之前的答案隐藏我的 TEXT 视图的键盘。

      仅供参考,我只是添加这个。

      // Para esconder el teclado al oprimir el fondo de la pantalla
      - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
          [self.answerTextView endEditing:YES];
      }
      

      theapplady提取

      【讨论】:

        猜你喜欢
        • 2016-03-19
        • 2012-04-22
        • 1970-01-01
        • 1970-01-01
        • 2020-04-17
        • 2011-05-20
        • 2016-01-08
        • 2019-06-21
        • 2021-07-20
        相关资源
        最近更新 更多