UIsearchBar输入键盘类型 [英] UIsearchBar input Keyboard Type

查看:251
本文介绍了UIsearchBar输入键盘类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何更改搜索栏的输入键盘类型吗?代码

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

searchController.searchBar.inputView = input

不能像在文本字段中那样工作.我已经知道searchBar的子视图是一个文本字段,但是我不知道如何访问该子视图来更改inputView.

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.

推荐答案

我想您要显示与标准键盘不同的键盘, 确保已将委托分配给键盘.

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
    }
}

我的自定义键盘类

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
    }

}

这篇关于UIsearchBar输入键盘类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆