Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在IOS中,如何在键盘顶部的键盘上添加“完成”按钮?_Ios_Objective C_Keyboard_Uikeyboard - Fatal编程技术网

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

在IOS中,如何在键盘顶部的键盘上添加“完成”按钮?,ios,objective-c,keyboard,uikeyboard,Ios,Objective C,Keyboard,Uikeyboard,我想在iOS中的右侧键盘顶部添加“完成”按钮,用于文本视图。请告诉我如何操作 我想实现与上述键盘类似的功能希望有帮助:) 然后添加TextViewDoneButtonPressed方法 -(void)yourTextViewDoneButtonPressed { [self.yourTextView resignFirstResponder]; } Swift 3: func setDoneOnKeyboard() { let keyboardToolbar = UITool

我想在iOS中的右侧键盘顶部添加“完成”按钮,用于文本视图。请告诉我如何操作

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

希望有帮助:)

然后添加TextViewDoneButtonPressed方法

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

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

可以使用故事板完成此操作:

  • 将UITextField添加到UIViewController视图
  • 在UIViewController的第一级中添加UIToolbar
  • 将UIBarButtonim添加到UIToolbar中
  • 使用IBOutlet将UItoolbar连接到代码
  • 使用iAction将UIBarButtonim连接到代码(如didClick所示)
  • 将UITextField委托给UIViewController
  • 在didClick函数中,结束编辑(查看.endEditing(true)
  • 在委托函数中,textField shouldbeginediting应为:textField.inputAccessoryView=toolbar,并返回true

  • Ramis的答案是,允许使用故事板创建键盘附件视图,这是一个很好的策略。毕竟是2017年!(对不起,我没有足够的分数投票给你。)

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

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

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

    添加UIToolBar作为自定义视图,其中将包含UIBarButtonim as Done按钮。

    这是一种更安全、更简洁的方式,可以将“完成”按钮添加到任何类型的键盘上。创建UIToolBar“添加完成”按钮,并设置任何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
    
    作用


    此解决方案适用于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()
            }
    
        }
    
    在扩展之前

    扩展之后


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

    使用自定义类解决Swift 4Swift 5。您可以在故事板和.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()
        }
    }
    

    您可以使用UIToolbar!创建一个自定义视图,然后在您的
    键盘屏幕上显示它
    通知方法中显示它…它还带有键盘高度,因此您知道在哪里放置BarYo与工具栏一起使用屏幕上方的barHow与IQKeyboardManager相关请添加pod文件,它将像上面的屏幕截图一样自动管理。检查此链接,了解如何隐藏键盘?@deepakkumar你是什么意思?如果要隐藏键盘,只需调用方法[self.yourTextView resignFirstResponder];若键盘被隐藏,那个么工具栏就不会被隐藏。请告诉我怎么做?我刚刚做了一个简单的测试,效果很好。但是tollbar并没有消失。wehn键盘已经好了。这很好,但是我正在尝试同样的UITextView?什么需要不同。我编辑了我的想法,但get无法分配UIButtonBarItem类型的值!打字UIView@adamprocter您应该将工具栏指定给textView.inputAccessoryView,而不是UIButtonBarItem.Awesome@Ramis(Y)非常感谢。同样适用于UITextView。谢谢调用时这样暴露
    doneAccessory
    getter将导致无限循环
    get{return self.doneAccessory}
    @anup gupta它工作得很好,但我得到的唯一问题是此警告
    通过此函数的所有路径将在这一行
    get上调用自身{
    此解决方案在Xcode 10.2和Swift 5.0中运行。感谢Kirill。非常好的解决方案thanx用于共享!!也可以作为TextView:-)
    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)
    }
    
    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()
        }
    }