【问题标题】:How to add done button on keyboard on top of keyboard in IOS?如何在IOS键盘顶部的键盘上添加完成按钮?
【发布时间】:2016-03-19 13:17:07
【问题描述】:

我想在 iOS 右侧的键盘顶部添加 Done 按钮作为 textView。请告诉我该怎么做?

我想实现类似于上面键盘的东西

【问题讨论】:

  • 你可以使用 UIToolbar!
  • 创建一个自定义视图,然后在您的keyboardOnScreen 通知方法中显示它......它也带有键盘高度,所以你知道在哪里放置栏
  • 你如何使用工具栏
  • 以上屏幕与 IQKeyboardManager 有关,请添加 pod 文件,它会自动管理,如上图所示。检查此链接github.com/hackiftekhar/IQKeyboardManager,进行设置

标签: ios objective-c keyboard uikeyboard


【解决方案1】:

希望有帮助:)

UIToolbar* keyboardToolbar = [[UIToolbar alloc] init];
[keyboardToolbar sizeToFit];
UIBarButtonItem *flexBarButton = [[UIBarButtonItem alloc]
                                  initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                  target:nil action:nil];
UIBarButtonItem *doneBarButton = [[UIBarButtonItem alloc]
                                  initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                  target:self action:@selector(yourTextViewDoneButtonPressed)];
keyboardToolbar.items = @[flexBarButton, doneBarButton];
self.yourTextView.inputAccessoryView = keyboardToolbar;

然后添加 yourTextViewDoneButtonPressed 方法

-(void)yourTextViewDoneButtonPressed
{
    [self.yourTextView resignFirstResponder];
}

【讨论】:

  • 如何隐藏键盘?
  • @deepakkumar 你是什么意思?如果你想隐藏键盘,只需调用一个方法 [self.yourTextView resignFirstResponder];
  • 如果键盘被隐藏则工具栏没有被隐藏。请告诉怎么做?
  • 我刚刚做了一个简单的测试,效果很好。 postimg.org/image/tjxdwj3f3postimg.org/image/rquh87i8f
  • 但是收费栏没有消失。wehn 键盘消失了
【解决方案2】:

可以使用故事板来完成:

  1. 将 UITextField 添加到 UIViewController 视图。
  2. 在一级UIViewController中添加UIToolbar
  3. 将 UIBarButtonItem 添加到 UIToolbar。
  4. 使用 IBOutlet 将 UItoolbar 连接到代码。
  5. 使用 IBAction 将 UIBarButtonItem 连接到代码(与 didClick 一样)。
  6. 使 UITextField 将委托给 UIViewController。
  7. 在didClick函数结束编辑(view.endEditing(true))
  8. 在委托函数中 textFieldShouldBeginEditing 应该是:textField.inputAccessoryView = toolbar并返回true。李>

【讨论】:

  • 这很好,但是我想为 UITextView 做同样的事情?需要有什么不同。我编辑了我的想法,但得到了无法分配 UIButtonBarItem 类型的值!输入 UIView
  • @adamprocter 您应该将工具栏分配给 textView.inputAccessoryView,而不是 UIButtonBarItem。
  • 真棒@Ramis (Y) 非常感谢。
【解决方案3】:

斯威夫特 3:

func setDoneOnKeyboard() {
    let keyboardToolbar = UIToolbar()
    keyboardToolbar.sizeToFit()
    let flexBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let doneBarButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(InsertStatusVC.dismissKeyboard))
    keyboardToolbar.items = [flexBarButton, doneBarButton]
    self.fullNameTextField.inputAccessoryView = keyboardToolbar
}

@objc func dismissKeyboard() {
    view.endEditing(true)
}

【讨论】:

    【解决方案4】:

    此解决方案适用于 Swift3/4

    将此行作为扩展添加到您的项目中 你的问题解决了。

      extension UITextField{
    
            @IBInspectable var doneAccessory: Bool{
                get{
                    return self.doneAccessory
                }
                set (hasDone) {
                    if hasDone{
                        addDoneButtonOnKeyboard()
                    }
                }
            }
    
            func addDoneButtonOnKeyboard()
            {
                let doneToolbar: UIToolbar = UIToolbar(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
                doneToolbar.barStyle = .default
    
                let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
                let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction))
    
                let items = [flexSpace, done]
                doneToolbar.items = items
                doneToolbar.sizeToFit()
    
                self.inputAccessoryView = doneToolbar
            }
    
            @objc func doneButtonAction() {
                self.resignFirstResponder()
            }
    
        }
    

    扩展之前

    扩展之后

    【讨论】:

    • 也适用于 UITextView。谢谢!
    • 在调用时像这样暴露doneAccessory getter 会导致无限循环get{ return self.doneAccessory }
    • @anup-gupta 它工作得很好,但我得到的唯一问题是这个警告All paths through this function will call itself在这条线上get{
    【解决方案5】:

    Swift 4Swift 5 使用自定义类的解决方案。您可以在 Storyboard 和 .xib 文件中使用此类。

    class UITextFieldWithDoneButton: UITextField {
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            self.addDoneButtonOnKeyboard()
        }
    
        fileprivate func addDoneButtonOnKeyboard() {
            let doneToolbar: UIToolbar = UIToolbar(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
            doneToolbar.barStyle = .default
    
            let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
            let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction))
    
            let items = [flexSpace, done]
            doneToolbar.items = items
            doneToolbar.sizeToFit()
    
            self.inputAccessoryView = doneToolbar
        }
    
        @objc fileprivate func doneButtonAction() {
            self.resignFirstResponder()
        }
    }
    

    【讨论】:

    • 这个解决方案在 Xcode 10.2 和 Swift 5.0 中有效。谢谢基里尔。
    • 非常好的解决方案thanx分享!!也可以用作 TextView :-)
    • 不错的解决方案!避免代码重复的最佳方法。
    【解决方案6】:

    Swift 4 的这个解决方案

    override func viewDidLoad() {
       super.viewDidLoad()
       //init toolbar
       let toolbar:UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0,  width: self.view.frame.size.width, height: 30))
       //create left side empty space so that done button set on right side
       let flexSpace = UIBarButtonItem(barButtonSystemItem:    .flexibleSpace, target: nil, action: nil)
       let doneBtn: UIBarButtonItem = UIBarButtonItem(title: “Done”, style: .done, target: self, action: Selector(“doneButtonAction”))
       toolbar.setItems([flexSpace, doneBtn], animated: false)
       toolbar.sizeToFit()
       //setting toolbar as inputAccessoryView
       self.textField1.inputAccessoryView = toolbar
       self.textField2.inputAccessoryView = toolbar
    }
    func doneButtonAction() {
       self.view.endEditing(true)
    }
    

    【讨论】:

      【解决方案7】:

      将 UIToolBar 添加为自定义视图,其中包含 UIBarButtonItem 作为完成按钮。

      这是将完成按钮添加到任何类型键盘的更安全、更简洁的方法。创建 UIToolBar 添加 Done Button 并设置任何 UITextField 或 UITextView 的 inputAccessoryView。 ]; }

      SWIFT 3

      var ViewForDoneButtonOnKeyboard = UIToolbar()
      ViewForDoneButtonOnKeyboard.sizeToFit()
      var btnDoneOnKeyboard = UIBarButtonItem(title: "Done", style: .bordered, target: self, action: #selector(self.doneBtnFromKeyboardClicked))
      ViewForDoneButtonOnKeyboard.items = [btnDoneOnKeyboard]
      myTextField.inputAccessoryView = ViewForDoneButtonOnKeyboard
      

      功能

        @IBAction func doneBtnfromKeyboardClicked (sender: Any) {
           print("Done Button Clicked.")
          //Hide Keyboard by endEditing or Anything you want.
          self.view.endEditing(true)
        }
      

      【讨论】:

      • “创建 UIToolbar”:代码中的文件?一个 UIBuilder 对象?更具体。
      【解决方案8】:

      Ramis 的回答是允许使用情节提要创建键盘附件视图,这是一个很好的策略。毕竟是2017年! (抱歉,我没有足够的积分来支持你。)

      让我补充一点答案(以遵守 SO 规则)。我有多个文本字段的视图。在执行了 Ramis 的每个步骤后,我以这种方式将附件视图附加到所有文本字段:

      -(void)textFieldDidBeginEditing:(UITextField *)textField
      {
      _activeTextField = textField;
      [_activeTextField setInputAccessoryView:_iboKeyboardTools];
      }
      

      与在代码中实现所有这些相比,这太容易了!只需在 Storyboard 中构建附件视图,然后一次又一次地附加它!

      【讨论】:

        猜你喜欢
        • 2014-03-05
        • 2021-07-20
        • 2012-04-22
        • 2013-12-10
        • 1970-01-01
        • 2011-05-20
        • 1970-01-01
        • 2023-03-15
        • 2022-11-01
        相关资源
        最近更新 更多