Toolbar with “Previous” and “Next” for Keyboard inputAccessoryView

后端 未结 7 1761
面向向阳花
面向向阳花 2020-12-02 20:53

I\'ve been trying to implement this toolbar, where only the \'Next\' button is enabled when the top textField is the firstResponder and only the \'Previous\' button is enabl

7条回答
  •  难免孤独
    2020-12-02 21:45

    Updated for Swift 3.0

    lazy var inputToolbar: UIToolbar = {
        var toolbar = UIToolbar()
        toolbar.barStyle = .default
        toolbar.isTranslucent = true
        toolbar.sizeToFit()
    
        var doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ContactViewController.inputToolbarDonePressed))
        var flexibleSpaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        var fixedSpaceButton = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
    
        var nextButton = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(ContactViewController.keyboardNextButton))
        var previousButton = UIBarButtonItem(title: "Previous", style: .plain, target: self, action: #selector(ContactViewController.keyboardPreviousButton))
    
        toolbar.setItems([fixedSpaceButton, previousButton, fixedSpaceButton, nextButton, flexibleSpaceButton, doneButton], animated: false)
        toolbar.isUserInteractionEnabled = true
    
        return toolbar
    }()
    

    And then:

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        textField.inputAccessoryView = inputToolbar
        return true
    }
    

    Remember to change thange "ContactViewController" to the name of your View Controller.

提交回复
热议问题