Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Tuan Nguyen
3,365 PointsDismiss custom keyboard programmatically in iOS
I am new to this forum. I am having problem creating a custom keyboard programmatically for my app only (no third-party keyboard installation). I successfully created a custom inputView and inserted UIButton. I wrote dismissKeyboard function to dismiss the keyboard but it did not work. Does anyone know how to fix or have another way to do it? Any help will be appreciated.
import UIKit
class ViewController: UIInputViewController, UITextFieldDelegate {
@IBOutlet weak var textfield: UITextField!
var keyBoardView: UIInputView!
var myFirstButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
keyBoardView = UIInputView(frame: CGRectMake(0, 0, view.frame.size.width, 100), inputViewStyle: .Keyboard)
myFirstButton = UIButton(frame: CGRectMake(100, 0, 100, 50))
myFirstButton.setTitle("1", forState: .Normal)
myFirstButton.addTarget(self, action: "dismissAction:", forControlEvents: .TouchUpInside)
keyBoardView.addSubview(myFirstButton)
self.textfield.delegate = self
textfield.inputView = keyBoardView
func dismissAction(sender: UIButton!) {
self.dismissKeyboard()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldShouldReturn(_textField: UITextField!) -> Bool {
var nextTag = _textField.tag + 1
var nextResponder = _textField.superview?.viewWithTag(nextTag)
if ((nextResponder) != nil) {
nextResponder?.becomeFirstResponder()
}
else {_textField.resignFirstResponder()}
return true;
}
}