Solutions: Making a Calculator (Adding Buttons to Numpad)

Adding Buttons to UIKit’s Numpad

Screen Shot 2015-08-20 at 10.49.21 AM
Xcode Statistician from the Scrivener team at Literature and Latte

On a current project, users need to enter their locations by latitude andlongitude. True, the real world use of such a feature is dubious (do you know, right now, your latitude and longitude to four digits? Or the lat/lng of your destination? Yes? Well, my response to you, dear reader, is STOP DRIVING while reading blogs!). However, to make a simple calculator — or even more basically, to enter a number and then Return — you will need more than the UIKit-provided numpad.

Problem

You want to make a vanilla calculator. You would like to enter numbers to a UITextField without the default alphanumeric keypad, but rather with a number pad. However, the UIKeyboardTypeNumberPad was “designed for PIN entry,” and just contains the digits.

Solution

Add a custom UIToolbar on top of the default Numpad with the button(s) you require.

How It Works

(This solution is but One of Many.)

UIToolbar, which normally appears at the bottom of the screen, can be added to the inputAccessoryView belonging to the UITextField in question. After wiring up the storyboard, we will: (more…)