It is possible to show keyboard without using UITextField and UITextView iphone app?

前端 未结 6 1255
执笔经年
执笔经年 2021-01-31 15:53

I am working in iPhone messaging based app.

I want to show keyboard with keyboard inputAccessoryView in keyboard without using UITextView and

6条回答
  •  不要未来只要你来
    2021-01-31 16:22

    Make a View, Label or what ever to conform to UIKeyInput. In this case a UIView

    Sub class a UIView:

    import UIKit
    
    class KeyInputView: UIView {
       var _inputView: UIView?
    
       override var canBecomeFirstResponder: Bool { return true }
       override var canResignFirstResponder: Bool { return true }
    
       override var inputView: UIView? {
           set { _inputView = newValue }
           get { return _inputView }
       }
    }
    
    // MARK: - UIKeyInput
    //Modify if need more functionality
    extension KeyInputView: UIKeyInput {
        var hasText: Bool { return false }
        func insertText(_ text: String) {}
        func deleteBackward() {}
    }
    

    Setup your View, in this case with a picker (in viewDidLoad or where ever)

    let languangePicker = UIPickerView()
    languangePicker.dataSource = self
    languangePicker.delegate = self
    keyInputView.inputView = languangePicker
    

    To show:

    keyInputView.becomeFirstResponder()
    

    To hide:

    keyInputView.resignFirstResponder()
    

    Set picker data, from datasource (compiler force u to do it)

    Grab data picker events from delegate

提交回复
热议问题