Guest User

Scrolling the view if the keyboard is covering it.

a guest
Feb 26th, 2016
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  SignUpViewController.swift
  3. //  Culpability
  4. //
  5. //  Created by Lazar Nikolov on 2/26/16.
  6. //  Copyright © 2016 Lazar Nikolov. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class SignUpViewController: UIViewController {
  12.  
  13.     @IBOutlet weak var tfUsername: UITextField!
  14.     @IBOutlet weak var tfPassword: UITextField!
  15.     @IBOutlet weak var tfConfirmPassword: UITextField!
  16.    
  17.     override func viewDidLoad() {
  18.         super.viewDidLoad()
  19.        
  20.         NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
  21.         NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
  22.  
  23.     }
  24.    
  25.     func keyboardWillShow(notification: NSNotification) {
  26.         if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
  27.             if self.tfUsername.isFirstResponder() {
  28.                 let usernameBottomY = self.tfUsername.frame.origin.y + self.tfUsername.frame.height
  29.                
  30.                 if usernameBottomY <= self.view.frame.height && usernameBottomY >= (self.view.frame.height - keyboardSize.height) {
  31.                     // Scroll it
  32.                     let verticalDifference = usernameBottomY - (self.view.frame.height - keyboardSize.height)
  33.                     UIView.animateWithDuration(0.3, animations: {
  34.                         self.view.frame.origin.y -= verticalDifference
  35.                     })
  36.                 }
  37.             } else if self.tfPassword.isFirstResponder() {
  38.                 let passwordBottomY = self.tfPassword.frame.origin.y + self.tfPassword.frame.height
  39.                
  40.                 if passwordBottomY <= self.view.frame.height && passwordBottomY >= (self.view.frame.height - keyboardSize.height) {
  41.                     // Scroll it
  42.                     let verticalDifference = passwordBottomY - (self.view.frame.height - keyboardSize.height)
  43.                     UIView.animateWithDuration(0.3, animations: {
  44.                         self.view.frame.origin.y -= verticalDifference
  45.                     })
  46.                 }
  47.             } else if self.tfConfirmPassword.isFirstResponder() {
  48.                 let confirmPasswordBottomY = self.tfConfirmPassword.frame.origin.y + self.tfConfirmPassword.frame.height
  49.                
  50.                 if confirmPasswordBottomY <= self.view.frame.height && confirmPasswordBottomY >= (self.view.frame.height - keyboardSize.height) {
  51.                     // Scroll it
  52.                     let verticalDifference = confirmPasswordBottomY - (self.view.frame.height - keyboardSize.height)
  53.                     UIView.animateWithDuration(0.3, animations: {
  54.                         self.view.frame.origin.y -= verticalDifference
  55.                     })
  56.                 }
  57.             }
  58.         }
  59.     }
  60.    
  61.     func keyboardWillHide(notification: NSNotification) {
  62.         if self.view.frame.origin.y < 0 {
  63.             UIView.animateWithDuration(0.3, animations: {
  64.                 self.view.frame.origin.y = 0
  65.             })
  66.         }
  67.     }
  68. }
Add Comment
Please, Sign In to add comment