繁体   English   中英

自定义inputView执行。 除非打开默认键盘,否则不会出现自定义inputView

[英]Custom inputView performing. Custom inputView doesn't appear unless default keyboard is opened

我有一个问题,除非打开键盘,否则无法执行自定义inputView。 我有2个UITextField,其中之一具有自定义inputView。 我们称它们为tf1和tf2。 tf2具有自定义的inputView。 如果我先点击tf2,则什么也没有发生。 如果我先点击tf1,然后出现默认键盘,然后单击tf2,则也会出现自定义inputView。 如果屏幕上没有键盘,则不会出现自定义inputView。 如果屏幕上有键盘,则可以显示自定义inputView。 为什么?

我如何分配inputview如下所示:

let numPad = KeyboardViewController(nibName:"KeyboardView",bundle: NSBundle.mainBundle())
let numPadFrame = CGRectMake(0, 0, 1024, 352)

override func viewDidLoad() {
    super.viewDidLoad()

    customKeyboard = numPad.view
    customKeyboard.frame = numPadFrame
    tf2.inputView = customKeyboard

终于我做到了。 我已经从viewDidLoad中删除了有关加载笔尖等的代码,并为其创建了一个函数。

这里的功能

func loadInterface() {

    // load the nib file
    let calculatorNib = UINib(nibName: "KeyboardView", bundle: NSBundle.mainBundle())
    // instantiate the view
    let calculatorView = calculatorNib.instantiateWithOwner(self, options: nil)[0] as! UIView

    self.view.frame = CGRectMake(0, 0, 1024, 352)
    let objects = calculatorNib.instantiateWithOwner(self, options: nil)
    self.view = objects[0] as! UIView
    self.inputView?.addSubview(calculatorView)


}

并在viewDidLoad中调用此函数。

    override func viewDidLoad() {
        super.viewDidLoad()

        loadInterface()

        self.view.autoresizingMask = UIViewAutoresizing.FlexibleHeight
        self.view.translatesAutoresizingMaskIntoConstraints = false


//        let nib = UINib(nibName: "KeyboardView", bundle: NSBundle.mainBundle())
//        self.view.frame = CGRectMake(0, 0, 1024, 352)
//        let objects = nib.instantiateWithOwner(self, options: nil)
//        view = objects[0] as! UIView;
        var counter = 0
        for view in self.view.subviews {
            if let btn = view as? UIButton {
                btn.layer.cornerRadius = 5.0
                btn.translatesAutoresizingMaskIntoConstraints = false
                btn.tag = counter
                counter++

            }
        }
    }

它解决了所有问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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