I have a problem that I cannot perform custom inputView unless keyboard is opened. I have 2 UITextField one of them has custom inputView. Let's call them tf1 and tf2. tf2 has custom inputView. if I tap tf2 first nothing happening. If I tap tf1 first and default keyboard appears and then when I click tf2 custom inputView appears too. If there is no keyboard on the screen custom inputView doesn't appear. If there is a keyboard on the screen custom inputView can appear. Why?
How I assign inputview is shown below :
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
Finally I made it. I've deleted the codes about loading nib etc. from viewDidLoad and created a function for it.
Here the function
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)
}
and call this function in 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++
}
}
}
And it solved everything.