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

iOS Build an Interactive Story App with Swift Personalizing the Story Dismissing the Keyboard

Anna Korol
Anna Korol
3,772 Points

No keyboard showed, when I click on the field. Can anyone spot what is wrong with my code?

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var textFieldBottomConstraint: NSLayoutConstraint!

    override func viewDidLoad() {

        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name: Notification.Name.UIKeyboardWillShow, object: nil)


    override func didReceiveMemoryWarning() {
        // Dispose of any resources that can be recreated.
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "StartAdventure" {

            do {
                if let name = nameTextField.text {
                    if name == "" {
                        throw AdventureError.nameNotProvided
                    } else {
                        guard let pageController = segue.destination as? PageController else
                            { return }
                        pageController.page = Adventure.story(withName: name)
            } catch AdventureError.nameNotProvided {
                let alertController = UIAlertController(title: "Name Not Provided", message: "Provide a name to start the story", preferredStyle: .alert)
                let action = UIAlertAction(title: "OK", style: .default, handler: nil)

                present(alertController, animated: true, completion: nil)
            } catch let error {

    @objc func keyboardWillShow(_ notification: Notification) {
        if let info = notification.userInfo, let keyboardFrame = info [UIKeyboardFrameEndUserInfoKey] as? NSValue {
            let frame = keyboardFrame.cgRectValue
            textFieldBottomConstraint.constant = frame.size.height + 10

            UIView.animate(withDuration: 0.8) {

    deinit {

extension ViewController: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        return true


3 Answers

In the newest versions of the simulator you have to toggle the keyboard manually. Go to hardware > Keyboard > Toggle Software Keyboard.

30,362 Points

Are you using the simulator? I remember having some issues there while everything was fine on my hardware device.

This didn't work in the simulator, but it did work on my device like you stated.
Interesting! Wonder why that happens like that...

Thanks for this!

Joey Liu
Joey Liu
3,982 Points

If the keyboard is not shown on stimulator, you can press "command⌘ + k" and it will pop up.