如何在 IOS 中的键盘顶部的键盘上添加完成按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34260592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:22:57  来源:igfitidea点击:

How to add done button on keyboard on top of keyboard in IOS?

iosobjective-ckeyboarduikeyboard

提问by TechChain

I want to add the Donebutton on the top of keyboard on right side in iOS for a textView.Please tell me how can i do that?

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

enter image description here

在此处输入图片说明

I want to achieve something similar to the above keyboard

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

回答by Cong Tran

Hope this help :)

希望这有帮助:)

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;

and then add yourTextViewDoneButtonPressed method

然后添加 yourTextViewDoneButtonPressed 方法

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

回答by Ramis

It can be done using storyboard:

它可以使用故事板完成:

  1. Add UITextField to UIViewController view.
  2. Add UIToolbar in the first level of UIViewController
  3. Add UIBarButtonItem into the UIToolbar.
  4. Connect UItoolbar to the code using IBOutlet.
  5. Connect UIBarButtonItem to the code using IBAction (as didClick).
  6. Make the UITextField will be delegating to UIViewController.
  7. In the didClick function end editing (view.endEditing(true))
  8. In the delegate function textFieldShouldBeginEditing should be: textField.inputAccessoryView = toolbarand returns true.
  1. 将 UITextField 添加到 UIViewController 视图。
  2. 在 UIViewController 的第一层添加 UIToolbar
  3. 将 UIBarButtonItem 添加到 UIToolbar。
  4. 使用 IBOutlet 将 UItoolbar 连接到代码。
  5. 使用 IBAction(如 didClick)将 UIBarButtonItem 连接到代码。
  6. 使 UITextField 将委托给 UIViewController。
  7. 在 didClick 函数中结束编辑(view.endEditing(true)
  8. 在委托函数中 textFieldShouldBeginEditing 应该是:textField.inputAccessoryView = toolbar并返回true

enter image description here

在此处输入图片说明

回答by Danny182

Swift 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)
}

回答by Anup Gupta

This Solution For Swift3/4

这个适用于Swift3/4 的解决方案

Add This line To your Project As a extensionYour Problem is solved.

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

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

    }

Before extension

延期

enter image description here

在此处输入图片说明

After extension

延伸

enter image description here

在此处输入图片说明

回答by Shoukat Mirza

This Solution For Swift 4

这个 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)
}

回答by Kirill Kudaev

Solution for Swift 4and Swift 5using a custom class. You can use this class in your Storyboard and .xib files.

使用自定义类的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()
    }
}

回答by ViJay Avhad

Add UIToolBar as custom view that will have UIBarButtonItem as Done button in it.

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

This is safer and cleaner way to add Done button to any Type of Keyboard. Create UIToolBar add Done Button to it and set inputAccessoryView of any UITextField or UITextView.]; }

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

SWIFT 3

斯威夫特 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

Function

功能

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

回答by Steve W.

The answer from Ramis that allows the keyboard accessory view to be created using the Storyboard is a great strategy. It is 2017 after all! (Sorry, but I do not have enough points to upvote you.)

Ramis 的答案是允许使用 Storyboard 创建键盘附件视图是一个很好的策略。毕竟是2017年!(对不起,我没有足够的积分来给你点赞。)

Let me add a little to the answer (to keep within the SO rules). I have a view with multiple textFields. After implementing each of Ramis' steps I attached the accessoryView to all of the textFields in this way:

让我在答案中添加一点(以保持在 SO 规则内)。我有一个包含多个 textField 的视图。在实现 Ramis 的每个步骤后,我以这种方式将附件视图附加到所有文本字段:

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

So easy compared with implementing all this in code! Just build the accessory view in Storyboard- and attach it again and again!

与在代码中实现所有这些相比,如此简单!只需在 Storyboard 中构建附件视图 - 并一次又一次地附加它!