简体   繁体   中英

iOS 9 UIInputViewController dismissKeyboard

I have a custom UIInputViewController , let's call it MyInputViewController . I set the a text field's input view to the view of a singleton instance of my input controller. Inserting, deleting and moving the cursor works fine however when I call self.dismissKeyboard() in the input view controller (method is called, print() writes to the console), it doesn't do anything.

What could be wrong? It got it to work a few days ago however I couldn't remember what might be the error.

class MyInputController: UIInputViewController {

// SINGLETON:
static let keyboard: MyInputController = MyInputController()

class func setSharedKeyboardForTextField(textField: UITextField) {
    textField.inputAccessoryView = nil
    textField.inputView = keyboard.view
}

// MARK: initializers
private init() {
    super.init(nibName: nil, bundle: nil)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func loadView() {
    super.loadView()
    self.view.translatesAutoresizingMaskIntoConstraints = false=
}

override func viewDidLoad() {
    //set up buttons
}

// MARK: helper functions
func inputText() -> String {
    return (self.textDocumentProxy.documentContextBeforeInput ?? "") + (self.textDocumentProxy.documentContextAfterInput ?? "")
}

// MARK: animations
private func grow(sender: MathematicalKeyboardKey) {
    sender.superview?.bringSubviewToFront(sender)
    UIView.animateWithDuration(0.1) { () -> Void in
        sender.transform = scaleTransform
    }
}

private func shrink(sender: MathematicalKeyboardKey) {
    sender.superview?.sendSubviewToBack(sender)
    UIView.animateWithDuration(0.1) { () -> Void in
        sender.transform = CGAffineTransformIdentity
    }
}

// MARK: actions
func keyTouched(sender: MathematicalKeyboardKey) {
    // works
    grow(sender)
}

func keyExit(sender: MathematicalKeyboardKey) {
    // works
    shrink(sender)
}

func resign(sender: MathematicalKeyboardKey) {
    // called but doesn't work
    dismissKeyboard()
    shrink(sender)
}

func keyPressed(sender: MathematicalKeyboardKey) {
    // works
    self.textDocumentProxy.insertText(sender.insertion)
    shrink(sender)
}

func remove(sender: MathematicalKeyboardKey) {
    // works
    self.textDocumentProxy.deleteBackward()
    shrink(sender)
}

func clear(sender: MathematicalKeyboardKey) {
    // works
    while self.textDocumentProxy.hasText() {
        self.textDocumentProxy.deleteBackward()
    }
    shrink(sender)
}

func moveLeft(sender: MathematicalKeyboardKey) {
    // works
    self.textDocumentProxy.adjustTextPositionByCharacterOffset(-1)
    shrink(sender)
}

func moveRight(sender: MathematicalKeyboardKey) {
    // works
    self.textDocumentProxy.adjustTextPositionByCharacterOffset(1)
    shrink(sender)
}

}

You can use ;

 resignFirstResponder()

method instead of

 dismissKeyboard()

method.

@IBAction func hideKeyboard(sender: UIButton) {

    resignFirstResponder()

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM