0

我有一个问题,除非打开键盘,否则我无法执行自定义 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
4

1 回答 1

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

            }
        }
    }

它解决了一切。

于 2016-01-11T14:50:42.187 回答