Swift Keyboard Control for UITextFields and UITextViews
Swift Keyboard Control for UITextFields and UITextViews

Swift Keyboard Control for UITextFields and UITextViews

Mazen Kilani
AppleCommunity
Published in
3 min readApr 25, 2021

--

Keyboard Control as User Navigates UITextFields and UITextViews on Table View Form

Scenario

Our app will allow users to tap between UITextFields and UITextViews and control the keyboard display (hide or show) as required. The code will ensure that the keyboard does not override (hide) the UITextField or UITextView the user has in focus.

Technique

We shall utilise the Delegate feature so the view, as the delegate to the UITextFields and UITextViews, can control the impact of the keyboard display on the positions of UITextFields and UITextViews. Then we shall use GestureRecognizer to detect user gestures and check the UITextField or UITextView BeginEditing and EndEditing conditions and animate the fields or views to move them up or down, depending on the keyboard display status.

Audience

The article is for Swift developers who seek complete, integrated, proven, code-centric solutions to speed up their development projects.

App Model

We based the article on the AppsGym Books model app, published on Apple’s App Store (as 8Books), and you can download the complete Xcode project on AppsGym.com, for free.

User Interfaces

NewBookTableViewController.swift and EditBookTableViewController.swift control the layout, order, and navigation of the corresponding UITableViewControllers.

Logic

NewBookTableViewController.swift contains keyboard control logic, which monitors and detects user taps on the form fields and reacts accordingly.

There are 7 UITextFields on the New Book view: Title, Author, Series, No in Series, Category (Genre), Source, and Format. Each field has a Tag on the Storyboard (e.g., titleTextField.tag = 1) or programmatically by code (see below), and each behaviour is governed by 3 textField functions, which we shall utilise to control the textField attributes and movement by controlling the becomeFirstResponder() function.

textFieldShouldReturn(..), textFieldDidBeginEditing(..), and textFieldDidEndEditing(..)

There are 2 UITextViews on the New Book view: Synopsis and Notes. We shall use 2 textView functions to control their behaviour: textViewDidBeginEditing (..) and textViewDidEndEditing(..).

Code

We shall separate the code of UITextFields and UITextView to make it easier to focus on each group keyboard control.

UITextFields

NewBookTableViewController.swift Initialise Variables

NewBookTableViewController.swift viewDidLoad()

NewBookTableViewController.swift Keyboard Control

UITextViews

NewBookTableViewController.swift Initialise Variables

NewBookTableViewController.swift viewDidLoad()

NewBookTableViewController.swift Keyboard Control

Keyboard Height

The UITextFields code snippets above relied on the default behaviour of the UITableView and becomeFirstResponder() function, while the UITextViews code used a simple moveValue of 100, which would suffice for most scenarios. However, you may like to use a more accurate measure based on the actual keyboard height of the device.

The code snippet below shows how to obtain the Keyboard Height. You can optionally use keyboardHeight in the UITextField functions for more precise controls.

UITextFields Using Keyboard Height

If you want to have a more precise control of moving the UITextFields up/down upon keyboard show/hide, then review the following code, which does not use the becomeFirstResponder() behaviour.

Popular Third-Party Tools

The (soft) keyboard covering/hiding text fields and views is a common issue for most applications, hence developers have published few techniques on GitHub. The most frequently cited is IQKeyboardManager, by hackiftekhar, which is a brilliant software library and needs no further customisation. If it fulfils your purpose then all is well, but if you prefer manual code-driven solutions rather than third-party additions, then try the above approach.

The article covered the complete logic and code to control the behaviour of TextFields and TextViews, so the keyboard does not obscure the relevant fields or views behind it. Hope you find it useful in your app. Thanks for reading!

--

--

Mazen Kilani
AppleCommunity

I published 47 Swift iOS apps/games and 2 Flutter Android apps. I share complete Xcode projects, free (no ads), at AppsGym.com.