Question: I know how to dismiss a keyboard, I use this extension:

```
extension UIViewController
{
    func hideKeyboardWhenTappedAround()
    {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
        view.addGestureRecognizer(tap)
    }

    func dismissKeyboard()
    {
        view.endEditing(true)
    }
}

```

And called `hideKeyboardWhenTappedAround` in `viewDidLoad`

But my problem now is I added a `UITextField` to a `navigationBar`, and this extension no longer works!

This is how I added the `UITextField`:

```
    let textField = UITextField(frame: CGRectMake(0,0,textfieldW,0.8*ram.navigationBarHeight) )
    textField.borderStyle = UITextBorderStyle.RoundedRect
    textField.center.y = centerView.center.y
    centerView.addSubview(textField)
    self.navigationItem.titleView = centerView

```

How to dismiss a keyboard brought from a `UITextField` that lurks in a navigation bar?

[![enter image description here](https://i.stack.imgur.com/NTwM3.png)](https://i.stack.imgur.com/NTwM3.png)

Answer: 
Make a reference to this text field, like:

```
var navigationBarField : UITextField?

```

Then initialize it:

```
navigationBarField = UITextField(frame: CGRectMake(0,0,textfieldW,0.8*ram.navigationBarHeight) )
textField.borderStyle = UITextBorderStyle.RoundedRect
textField.center.y = centerView.center.y
centerView.addSubview(navigationBarField)
self.navigationItem.titleView = centerView

```

And when you want to remove keyboard call:

```
navigationBarField?.resignFirstResponder()

```
Instead of declare instance you just need to call `endEditing` method of `navigationController's` `view` also like this

```
func dismissKeyboard()
{   
    navigationController?.view.endEditing(true)
    view.endEditing(true)
}

```