Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
739 views
in Technique[技术] by (71.8m points)

ios - UIsearchBar input Keyboard Type

Does anyone know how to change the input keyboard type for the searchbar? The code

searchController.searchBar.inputView = input

doesn't work like in a text field. I have read that the subview of the searchBar is a textfield but I don't know how to access that subview to change the inputView.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you want to display different keyboard than standard, Make sure you have assign delegate to keyboard.

class ViewController: UIViewController, UISearchBarDelegate, KeyboardDelegate
{
    @IBOutlet weak var searchBar: UISearchBar!

    override func viewDidLoad() {
 let keyboardView = KeyboardView(frame: CGRect(x: 0, y: 0, width: 375, height: 165))
        keyboardView.delegate = self
        let searchTextField = searchBar.value(forKey: "_searchField") as! UITextField
        searchTextField.inputView = keyboardView
    }

    func keyWasTapped(text: String) {
        searchBar.text = text
    }
}

My Custom Keyboard Class

protocol KeyboardDelegate: class {
    func keyWasTapped(text: String)
}

class KeyboardView: UIView {


    // This variable will be set as the view controller so that
    // the keyboard can send messages to the view controller.
    weak var delegate: KeyboardDelegate?


    // MARK:- keyboard initialization
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initializeSubviews()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        initializeSubviews()
    }

    func initializeSubviews() {
        let xibFileName = "KeyboardView" // xib extention not included
        let view = Bundle.main.loadNibNamed(xibFileName, owner: self, options: nil)?[0] as! UIView
        self.addSubview(view)
        view.frame = self.bounds
    }

    // MARK:- Button actions from .xib file    
    @IBAction func keyTapped(_ sender: UIButton) {
        // When a button is tapped, send that information to the
        // delegate (ie, the view controller)
        self.delegate?.keyWasTapped(text: sender.titleLabel!.text!) // could alternatively send a tag value
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...