---
title: "#299: Building Better Forms with UITextField-Navigation 🗺"
url: https://littlebitesofcocoa.com/299-building-better-forms-with-uitextfield-navigation
author: Jake Marsh
published: 2017-02-27
topics: ["Libraries","UIKit"]
description: "Building great forms for users to enter information on iOS is tough. There's a lot of small details which are easy to get wrong."
---

# #299: Building Better Forms with UITextField-Navigation 🗺

By Jake Marsh, February 27, 2017. HTML: https://littlebitesofcocoa.com/299-building-better-forms-with-uitextfield-navigation

Building great forms for users to enter information on iOS is tough. There's a lot of small details which are easy to get wrong. Today we'll begin looking at ways to improve this process.

First up, is **navigation**. 📍

Allowing for quick and simple navigation from one **form field** to the next can <i>dramatically</i> reduce friction for our users.

We'll use a great new library from [Thanh Pham](https://github.com/T-Pham) called [UITextField-Navigation](https://github.com/T-Pham/UITextField-Navigation) to pull this off. Let's get started.

[UITextField-Navigation](https://github.com/T-Pham/UITextField-Navigation) is built around the concept of telling each **field** which **field** should be <i>"next"</i> in the form.

Once we've done that for all of our fields, the library takes over and displays a navigation bar above the keyboard automatically:

<img src="https://media.littlebitesofcocoa.com/bite-assets/299-1.png" style="max-width: 375px; border-radius: 8px;" />

We can set up these relationships <i>super</i> easily in in **Interface Builder**:

<img src="https://media.littlebitesofcocoa.com/bite-assets/299-2.png" style="max-width: 373px; border-radius: 8px;" />

...or in code:

``` swift
let nameTextField = UITextField()
let bioTextView = UITextView()

nameTextField.nextNavigationField = bioTextView
```

Now, whenever a user begins editing a `UITextField` or `UITextView` in our app, a **navigation toolbar** will automatically be shown above the **keyboard**:

<img src="https://media.littlebitesofcocoa.com/bite-assets/299-3.gif" style="max-width: 375px; border-radius: 8px;" />

We can use `UIAppearance` to completely customize the look and feel of the **toolbar**:

``` swift
NavigationFieldToolbar.appearance().barStyle = .black
NavigationFieldToolbar.appearance().backgroundColor = UIColor.purple
NavigationFieldToolbarButtonItem.appearance().tintColor = UIColor.white
```

<img src="https://media.littlebitesofcocoa.com/bite-assets/299-4.png" style="max-width: 375px; border-radius: 8px;" />

Last but not least, we aren't limited to a <i>"standard"</i> set of buttons/items in the **toolbar**.

We'll use the `navigationFieldToolBar` optional **property** that the library adds to `UITextField` and `UITextView` to first customize some individual **properties** directly:

``` swift
guard let toolbar = nameTextField.navigationFieldToolbar else { return }

toolbar.barStyle = .default
toolbar.backgroundColor = UIColor.red
toolbar.previousButton.title = "Backward!"
toolbar.nextButton.title = "Onward!"
toolbar.doneButton.title = "Dismiss"
```

...then create our own array of **toolbar** items:

``` swift
let expandButton = UIBarButtonItem(
  title: "Expand",
  style: .plain,
  target: self,
  action: #selector(expandForm)
)

let flexible = UIBarButtonItem(
  barButtonSystemItem: .flexibleSpace, 
  target: nil,
  action: nil
)

toolbar.items = [
  toolbar.previousButton,
  toolbar.nextButton,
  flexible,
  expandButton,
  flexible,
  toolbar.doneButton
]
```

Neat!

Learn more about [UITextField-Navigation](https://github.com/T-Pham/UITextField-Navigation) at [git.io/textfieldnav](http://git.io/textfieldnav).
