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

后端 未结 7 1721
面向向阳花
面向向阳花 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:50

    Swift:

    lazy var inputToolbar: UIToolbar = {
        var toolbar = UIToolbar()
        toolbar.barStyle = .default
        toolbar.translucent = true
        toolbar.sizeToFit()
    
        var doneButton = UIBarButtonItem(title: "Done", style: .bordered, target: self, action: "inputToolbarDonePressed")
        var flexibleSpaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        var fixedSpaceButton = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
    
        var nextButton  = UIBarButtonItem(image: UIImage(named: "keyboardPreviousButton"), style: .bordered, target: self, action: "keyboardNextButton")
        nextButton.width = 50.0
        var previousButton  = UIBarButtonItem(image: UIImage(named: "keyboardNextButton"), style: .Bordered, target: self, action: "keyboardPreviousButton")
    
        toolbar.setItems([fixedSpaceButton, nextButton, fixedSpaceButton, previousButton, flexibleSpaceButton, doneButton], animated: false)
        toolbar.userInteractionEnabled = true
    
        return toolbar
        }()
    

    In UITextFieldDelegate

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

    0 讨论(0)
提交回复
热议问题