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

时间:2015-12-12 16:20:19

标签: ios swift uitextfield swift2.1 uiinputviewcontroller

我遇到一个问题,除非打开键盘,否则我无法执行自定义inputView。我有2个UITextField其中一个有自定义inputView。我们称之为tf1和tf2。 tf2有自定义inputView。如果我点击tf2首先什么也没发生。如果我先点击tf1并出现默认键盘,然后当我点击tf2自定义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

1 个答案:

答案 0 :(得分:3)

最后我做到了。我已从viewDidLoad中删除了有关加载nib等的代码,并为其创建了一个函数。

这里的功能

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

            }
        }
    }

它解决了一切。