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

时间:2015-12-14 05:51:24

标签: ios objective-c keyboard uikeyboard

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

enter image description here

我希望实现与上述键盘类似的功能

8 个答案:

答案 0 :(得分:30)

可以使用故事板来完成:

  1. 将UITextField添加到UIViewController视图。
  2. 在UIViewController的第一级添加UIToolbar
  3. 将UIBarButtonItem添加到UIToolbar。
  4. 使用IBOutlet将UItoolbar连接到代码。
  5. 使用IBAction将UIBarButtonItem连接到代码(如同点击)。
  6. 让UITextField委托给UIViewController。
  7. 在didClick函数结束编辑中( view.endEditing(true)
  8. 在委托函数中,textFieldShouldBeginEditing应为: textField.inputAccessoryView = toolbar 并返回 true
  9. enter image description here

答案 1 :(得分:26)

希望这有帮助:)

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

答案 2 :(得分:23)

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

答案 3 :(得分:8)

此解决方案适用于 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()
        }

    }

扩展程序

之前

enter image description here

扩展程序

之后

enter image description here

答案 4 :(得分:1)

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

这是将Done按钮添加到任何类型键盘的更安全,更清晰的方法。创建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)
  }

答案 5 :(得分:1)

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

答案 6 :(得分:0)

Ramis的答案允许使用故事板创建键盘配件视图是一个很好的策略。毕竟是2017年! (对不起,但我没有足够的积分来支持你。)

让我在答案中加一点(保持在SO规则范围内)。我有一个包含多个textFields的视图。实施Ramis'步骤我以这种方式将accessoryView附加到所有textField:

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

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

答案 7 :(得分:0)

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