Move View Behind Keyboard in iOS8 With Swift

When a user begins editing a text object the keyboard is diplayed. It is possible for the keyboard to be placed on top of the text object that the user wanted to edit. When this happens, you must adjust your content so that the target object remains visible. In this tutorial a Text Field on the bottom of the sceen will be edited, when the keyboard appears the Text Field will move upwards to keep it visible. This tutorial is built in iOS 8.1 and Xcode 6.1

Open Xcode and create a new Single View Application. For product name, use IOS8SwiftMoveViewKeyboardTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Go to the Storyboard and drag a Label and a Text Field to the main View. Give the Label a title of "Username:". The Storyboard should look like this.

Select the Assistant Editor and make sure the ViewController.swift is visible. Ctrl and drag from the Text Field to the ViewController class  and create the following Outlet.

Go to the ViewController.swift file and change the class declaration line 

class ViewController: UIViewController, UITextFieldDelegate {

The ViewController class must conform to the UITextFieldDelegate protocol. Change the viewDidLoad method 

override func viewDidLoad() {
    textField.delegate = self

The View Controller is set tp the Text Field's delegate. Add the delegate method textFieldShouldReturn

func textFieldShouldReturn(textField: UITextField) -> Bool {
     return true

When the Enter key is pressed on the keyboard the keyboard will be dismissed. When the content  is behind the keyboard this content must move up the height of the keyboard to make it visible. Add the following property.

var kbHeight: CGFloat!

Add the viewDidAppear method

override func viewWillAppear(animated:Bool) {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)

Notifications are an way to send messages between objects. iOS broadcasts various notifications related to the keyboard on the screen. In this tutorial we will use the UIKeyboardWillShowNotification and UIKeyboardWillHideNotification .To listen for notifications, an object must add itself as an observer. When the notifcation is send the corresponding selector method is called. Add the viewWillDisappear method

override func viewWillDisappear(animated: Bool) {

When the View will disappear the observer can be removed. Implement the keyboardWillShow method. Next, implement the keyboardWillShow method

func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        if let keyboardSize =  (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            kbHeight = keyboardSize.height

When the object receives the notification, additional info is send along in the form of a user information dictionary. We extract the keyboardsize from this dictionary.and set the kbheight property. Finally the animateTextField method is called which we will implement in a moment. First, implement the keyWillHide method

func keyboardWillHide(notification: NSNotification) {

This will also call the animateTextField method. 

func animateTextField(up: Bool) {
    var movement = (up ? -kbHeight : kbHeight)

    UIView.animateWithDuration(0.3, animations: {
            self.view.frame = CGRectOffset(self.view.frame, 0, movement)

The up parameter decides if the movement will be up or down in the animation. The animation will take place with a duration of 0.3 seconds. Build and Run the project, click inside the Text Field and watch the Text Field animate up when the keyboard appears. When the  Enter key is pressed the Text Field moves back to its original locations. 

You can download the source code of the IOS8SwiftMoveViewKeyboardTutorial at the ioscreator repository on Github.