Move view when a specific textfield is selected swift - ios

I am created a meme generator app to better learn Swift and Xcode. I am learning to move the view when the user interacts with a text field that would be obstructed by the keyboard. I have this functionality working, with one exception. The desired functionality is to have the view slide up when the user is entering text for the bottom textfield, bot the top. The view slides up regardless of the text field the user is interacting with.
How can I assign this functionality only to the bottom text field? Here is my source code:
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate {
#IBOutlet weak var imagePickerView: UIImageView!
#IBOutlet weak var cameraButton: UIBarButtonItem!
#IBOutlet weak var topText: UITextField!
#IBOutlet weak var bottomText: UITextField!
let memeTextAttributes:[String:Any] = [
NSAttributedStringKey.strokeColor.rawValue: UIColor.black,
NSAttributedStringKey.foregroundColor.rawValue: UIColor.white,
NSAttributedStringKey.font.rawValue: UIFont(name: "HelveticaNeue-CondensedBlack", size: 30)!,
NSAttributedStringKey.strokeWidth.rawValue: -5.0]
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
subscribeToKeyboardNotifications()
}
override func viewDidLoad() {
super.viewDidLoad()
// Diable camer a button if camera ource isn't available
cameraButton.isEnabled = UIImagePickerController.isSourceTypeAvailable(.camera)
topText.delegate = self
bottomText.delegate = self
topText.textAlignment = .center
bottomText.textAlignment = .center
topText.defaultTextAttributes = memeTextAttributes
bottomText.defaultTextAttributes = memeTextAttributes
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
unsubscribeFromKeyboardNotifications()
}
// MARK: Delegate Methods
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
imagePickerView.image = image
self.dismiss(animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.text = ""
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.topText.resignFirstResponder()
self.bottomText.resignFirstResponder()
return true
}
// MARK: Move the keyboard up when the bottom textfield is tapped
#objc func keyboardWillShow(_ notification:Notification) {
view.frame.origin.y = 0 - getKeyboardHeight(notification)
}
func getKeyboardHeight(_ notification:Notification) -> CGFloat {
let userInfo = notification.userInfo
let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue // of CGRect
return keyboardSize.cgRectValue.height
}
func subscribeToKeyboardNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: .UIKeyboardWillHide, object: nil)
}
func unsubscribeFromKeyboardNotifications() {
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)
}
// MARK: Move view down when keyboard is dismissed
#objc func keyboardWillHide(_ notification: Notification) {
view.frame.origin.y = 0
}
// MARK: IBActions
#IBAction func pickAnImageFromAlbum(_ sender: Any) {
let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.sourceType = .photoLibrary
present(pickerController, animated: true, completion: nil)
}
#IBAction func pickAnImageFromCamera(_ sender: Any) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
}

You can simply try this
//make a global textField to keep reference
var currentTappedTextField : UITextField?
//use this method to get tapped textField
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
currentTappedTextField = textField
return true
}
// now move view only when textfield is bottom
#objc func keyboardWillShow(_ notification:Notification) {
if(currentTappedTextField == bottomText){
view.frame.origin.y = 0 - getKeyboardHeight(notification)
}
}

Add view in scrollview.
Use
pod 'IQKeyboardManagerSwift'
It will automatically handle that. In app delegate write this code :
IQKeyboardManager.sharedManager().enable = true
IQKeyboardManager.sharedManager().keyboardDistanceFromTextField = 30.0
If you want for one textfield:
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField == toptextField {
IQKeyboardManager.sharedManager().enable = false
}
else {
IQKeyboardManager.sharedManager().enable = true
}
return true
}

Approach without using external framework:
Use a bottom constraint from the text field to the parent view.
Adjust the constant value based on whether the keyboard is shown or hidden.
Steps:
Create a bottom constraint from your text field to the parent view.
Set the constraint's constant to an initial desired value
Add store the constraint as a property in the view controller
Observe UIKeyboardDidShow notification and get the end frame of the keyboard. Use the negative height of the end frame as the bottom constraint's constant.
Similarly do the same in UIKeyboardWillHide and set the bottom constraint constant to the original constant value

You just need to observe Keyboard Notification in your viewDidLoad :
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillShow(_:)),
name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillHide),
name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
And declare selector methods to change your view constraint :
#objc
func keyboardWillShow(_ notification: Notification) {
if let keyboardHeight = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
yourViewBottomConstraint.constant = keyboardHeight.cgRectValue.height + constantHeight
UIView.animate(withDuration: 0.25, animations: {
self.view.layoutIfNeeded()
})
}
}
#objc
func keyboardWillHide() {
yourViewBottomConstraint.constant = constantHeight
UIView.animate(withDuration: 0.25, animations: {
self.view.layoutIfNeeded()
})
}
Just don't forget to implement UITextFieldDelegate :
extension ViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
}

Related

Unable to make View up when tapping on textfield in swift4

I have textfield and button inside containerview, now if i tap on textfield i need containerview has to up with keyboard
according to this answer
for bottom view
bottom = leading = trailing = 0, height = 80
and
i have created containerview bottom constraint to NSLayoutConstraint
and addd code like this: but i am unable to move container viewup, only keyboard coming.. view not comingup, where am i wrong
class MessageDetailsVCViewController: UIViewController {
#IBOutlet weak var messageTextfield: UITextField!
#IBOutlet weak var viewbottomConstraint: NSLayoutConstraint!
#IBOutlet weak var bottomContainerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: UIResponder.keyboardWillHideNotification, object: nil)
// Do any additional setup after loading the view.
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.bottomContainerView.superview?.setNeedsLayout()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func resignFirstResponder() -> Bool {
return true
}
#objc func handleKeyboardNotification(_ notification: Notification) {
if let userInfo = notification.userInfo {
let keyboardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
let isKeyboardShowing = notification.name == UIResponder.keyboardWillShowNotification
viewbottomConstraint?.constant = isKeyboardShowing ? -keyboardFrame!.height : 0
UIView.animate(withDuration: 0.5, animations: { () -> Void in
self.view.layoutIfNeeded()
})
}
}
}
You can try positive keyboardFrame!.height
viewbottomConstraint?.constant = isKeyboardShowing ? keyboardFrame!.height : 0

textFieldShouldReturn not being called after upgrade in Swift 4

I have a view controller with a few UITextFields on it. When a UITextField has focus, if I tap on the Return key on the keyboard, I go to the next UITextField. If the next UITextField is below the keyboard, I move the view up.
All was fine until yesterday when I upgraded the app to Swift 4, after a few changes. Now it's no longer working. I loaded the Swift 3 version and it's working just fine. The problem is I don't see any difference and I can't figure it out.
class ServerWizardVC: UIViewController, UITextViewDelegate, UIDocumentMenuDelegate, UIDocumentPickerDelegate, FileManagerDelegate
#IBOutlet weak var tfServerURL: UITextField!
#IBOutlet weak var tfServerUser: UITextField!
#IBOutlet weak var tfServerPassword: UITextField!
#IBOutlet weak var tfServerPort: UITextField!
override func viewDidLoad()
{
print("ServerWizardVC > viewDidLoad")
super.viewDidLoad()
tfServerURL.tag = 0
registerForKeyboardNotifications()
deregisterFromKeyboardNotifications()
}
override func viewWillAppear(_ animated: Bool)
{
print("ServerWizardVC > viewWillAppear")
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}
func registerForKeyboardNotifications()
{
print("ServerWizardVC > registerForKeyboardNotifications")
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
}
func deregisterFromKeyboardNotifications()
{
print("ServerWizardVC > deregisterFromKeyboardNotifications")
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
#objc func keyboardWillShow(notification:NSNotification)
{
print("ServerWizardVC > keyboardWillShow")
let userInfo:NSDictionary = notification.userInfo! as NSDictionary
let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
keyboardHeightValue = keyboardHeight
}
#objc func keyboardWasShown(notification: NSNotification)
{
print("ServerWizardVC > keyboardWasShown")
}
#objc func keyboardWillBeHidden (notification: NSNotification)
{
print("ServerWizardVC > keyboardWillBeHidden")
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
print("ServerWizardVC > textFieldShouldReturn")
if let nextField = tfServerURL.superview?.viewWithTag(textField.tag + 1) as? UITextField
{
nextField.becomeFirstResponder()
checkTextFieldPosition(tfTag: textField.tag + 1)
}
else
{
vMainView.frame.origin.y = 0
textField.resignFirstResponder()
}
return false
}
func checkTextFieldPosition(tfTag : Int)
{
print("ServerWizardVC > checkTextFieldPosition")
let keyboardTop = mainViewHeight - keyboardHeightValue
let tfServerPasswordTop = tfServerPassword.frame.origin.y
if(tfTag == 3)
{
if((keyboardTop < tfServerPasswordTop) && (vMainView.frame.origin.y == 0))
{
let yPosition = vMainView.frame.origin.y - keyboardHeightValue + 100
vMainView.frame.origin.y = yPosition
}
}
}
The reason I blame this on the Swift 4 upgrade is that I didn't even touch this class and everything else works fine.
Is there anything I'm missing?
textFieldShouldReturn is a UITextFieldDelegate function, but your class conforms to UITextViewDelegate instead. Replace the conformance with UITextFieldDelegate:
class ServerWizardVC: UIViewController, UITextFieldDelegate, UIDocumentMenuDelegate, UIDocumentPickerDelegate, FileManagerDelegate

iOS textViewDidChange is not triggered when predictive text is selected and added to text

When I tap on "Hello" or "He", textViewDidChange is not triggered.
How to detect when predictive text is selected?
Try using shouldChangeTextInRange instead of textViewDidChange
I tried it and it is triggered with predictive text
I had to use the NotificationCenter to get notified when a UITextView changes.
I've created an extension of UITextView to register/unregister for those notifications. Just remember to call unregister when you would no longer want to keep handling those changes (E.g. on viewWillDisappear).
import UIKit
extension UITextView {
public func registerTextViewNotifications() {
let center = NotificationCenter.default
center.addObserver(self,
selector: #selector(textViewDidChangeWithNotification(_:)),
name: UITextView.textDidChangeNotification,
object: nil)
}
public func unregisterTextViewNotifications() {
let center = NotificationCenter.default
center.removeObserver(self,
name: UITextView.textDidChangeNotification,
object: nil)
}
#objc private func textViewDidChangeWithNotification(_ notification: Notification) {
// Do something when edited
print("Text: \(String(describing: text))")
}
}
class ViewController: UIViewController {
#IBOutlet var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
textView.registerTextViewNotifications()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
textView.unregisterTextViewNotifications()
}
}
Do you have set textView.delegate ?
YourViewController<UITextViewDelegate>
After setup the textView, textView.delegate = self
func textViewDidChange(textView: UITextView):
func textViewDidChange(textView: UITextView) {
//textView(Sender)
if(textView == yourtextview) {
//do something
}
}

Next/Done button using Swift with textFieldShouldReturn

I have a MainView that adds a subview (signUpWindow) when a sign up button is pressed.
In my signUpWindow subview (SignUpWindowView.swift), I set up each field with a function, as an example:
func confirmPasswordText()
{
confirmPasswordTextField.frame=CGRectMake(50, 210, 410, 50)
confirmPasswordTextField.placeholder=("Confirm Password")
confirmPasswordTextField.textColor=textFieldFontColor
confirmPasswordTextField.secureTextEntry=true
confirmPasswordTextField.returnKeyType = .Next
confirmPasswordTextField.clearButtonMode = .WhileEditing
confirmPasswordTextField.tag=5
self.addSubview(confirmPasswordTextField)
}
I have the keyboard moving the signUpWindow up and down when it appears and disappears in the MainView.
SignUpWindowView implements the UITextFieldDelegate
My problem is that I am trying to configure the Next/Done button on the keyboard and am not sure which view (MainView or SignUpWindowView) to add the textFieldShouldReturn function. I have tried both, but can't even get a println to fire to test to see if the function is even being executed. Once I get the textFieldShouldReturn to fire, I am confident I can execute the necessary code to get the Next/Done buttons to do what I want, and will post the final solution to include the Next/Done function.
UPDATED to include an abbreviated version of SignUpWindowView.swift
import UIKit
class SignUpWindowView: UIView,UITextFieldDelegate {
let firstNameTextField:UITextField=UITextField()
let lastNameTextField:UITextField=UITextField()
override func drawRect(rect: CGRect){
func firstNameText(){
firstNameTextField.delegate=self
firstNameTextField.frame=CGRectMake(50, 25, 200, 50)
firstNameTextField.placeholder="First Name"
firstNameTextField.returnKeyType = .Next
self.addSubview(firstNameTextField)
}
func lastNameText(){
lastNameTextField.delegate=self
lastNameTextField.frame=CGRectMake(260, 25, 200, 50)
lastNameTextField.placeholder="Last Name"
lastNameTextField.returnKeyType = .Done
self.addSubview(lastNameTextField)
}
func textFieldShouldReturn(textField: UITextField!) -> Bool{
println("next button should work")
if (textField === firstNameTextField)
{
firstNameTextField.resignFirstResponder()
lastNameTextField.becomeFirstResponder()
}
return true
}
firstNameText()
lastNameText()
}
You need to implement UITextFieldDelegate in your class and set that object as the delegate for the UITextField. Then implement the method textFieldShouldReturn: like this:
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
if textField == someTextField { // Switch focus to other text field
otherTextField.becomeFirstResponder()
}
return true
}
In your example you are missing this line:
confirmPasswordTextField.delegate = self
If you have implemented the delegate of course.
I was attempting to test my textfields in the SignUpWindowView.swift, which is where all of the textFields are created. But, since I place SignUpWindowView into my MainViewController as a subview, all of my UITextField "handling" needed to be done in the MainView and NOT its subview.
So here is my entire code (at the moment) for my MainViewController, which handles moving my SignUpWindowView up/down when the keyboard is shown/hidden and then moves from one field to the next. When the user is in the last text field (whose keyboard Next button is now set to Done in the subview) the keyboard tucks away and the user can then submit the form with a signup button.
MainViewController:
import UIKit
#objc protocol ViewControllerDelegate
{
func keyboardWillShowWithSize(size:CGSize, andDuration duration:NSTimeInterval)
func keyboardWillHideWithSize(size:CGSize,andDuration duration:NSTimeInterval)
}
class ViewController: UIViewController,UITextFieldDelegate
{
var keyboardDelegate:ViewControllerDelegate?
let signUpWindow=SignUpWindowView()
let signUpWindowPosition:CGPoint=CGPointMake(505, 285)
override func viewDidLoad()
{
super.viewDidLoad()
// Keyboard Notifications
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
// set the textFieldDelegates
signUpWindow.firstNameTextField.delegate=self
signUpWindow.lastNameTextField.delegate=self
signUpWindow.userNameTextField.delegate=self
signUpWindow.passwordTextField.delegate=self
signUpWindow.confirmPasswordTextField.delegate=self
signUpWindow.emailTextField.delegate=self
}
func keyboardWillShow(notification: NSNotification)
{
var info:NSDictionary = notification.userInfo!
let keyboardFrame = info[UIKeyboardFrameEndUserInfoKey] as! NSValue
let keyboardSize = keyboardFrame.CGRectValue().size
var keyboardHeight:CGFloat = keyboardSize.height
let animationDurationValue = info[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
var animationDuration : NSTimeInterval = animationDurationValue.doubleValue
self.keyboardDelegate?.keyboardWillShowWithSize(keyboardSize, andDuration: animationDuration)
// push up the signUpWindow
UIView.animateWithDuration(animationDuration, delay: 0.25, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
self.signUpWindow.frame = CGRectMake(self.signUpWindowPosition.x, (self.signUpWindowPosition.y - keyboardHeight+140), self.signUpWindow.bounds.width, self.signUpWindow.bounds.height)
}, completion: nil)
}
func keyboardWillHide(notification: NSNotification)
{
var info:NSDictionary = notification.userInfo!
let keyboardFrame = info[UIKeyboardFrameEndUserInfoKey] as! NSValue
let keyboardSize = keyboardFrame.CGRectValue().size
var keyboardHeight:CGFloat = keyboardSize.height
let animationDurationValue = info[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
var animationDuration : NSTimeInterval = animationDurationValue.doubleValue
self.keyboardDelegate?.keyboardWillHideWithSize(keyboardSize, andDuration: animationDuration)
// pull signUpWindow back to its original position
UIView.animateWithDuration(animationDuration, delay: 0.25, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
self.signUpWindow.frame = CGRectMake(self.signUpWindowPosition.x, self.signUpWindowPosition.y, self.signUpWindow.bounds.width, self.signUpWindow.bounds.height)
}, completion: nil)
}
func textFieldShouldReturn(textField: UITextField) -> Bool
{
switch textField
{
case signUpWindow.firstNameTextField:
signUpWindow.lastNameTextField.becomeFirstResponder()
break
case signUpWindow.lastNameTextField:
signUpWindow.userNameTextField.becomeFirstResponder()
break
case signUpWindow.userNameTextField:
signUpWindow.passwordTextField.becomeFirstResponder()
break
case signUpWindow.passwordTextField:
signUpWindow.confirmPasswordTextField.becomeFirstResponder()
break
case signUpWindow.confirmPasswordTextField:
signUpWindow.emailTextField.becomeFirstResponder()
break
default:
textField.resignFirstResponder()
}
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
#IBAction func signup()
{
signUpWindow.frame=CGRectMake(signUpWindowPosition.x, signUpWindowPosition.y, 485,450)
signUpWindow.backgroundColor=UIColor.clearColor()
self.view.addSubview(signUpWindow)
}
}
Using tags makes it easier. Assign tags in ascending order to all the text fields you are using on your screen.
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let textTag = textField.tag+1
if let nextResponder = textField.superview?.viewWithTag(textTag) as UIResponder
{
//textField.resignFirstResponder()
nextResponder.becomeFirstResponder()
}
else {
// stop editing on pressing the done button on the last text field.
self.view.endEditing(true)
}
return true
}
You connect the DidEndOnExit(I wrote this from memory so maybe its not called this exactly but similar) UIControl event using an #IBAction and in that func you use textF.resignFirstResponder() or .becomeFirstResponder()
EDIT
UITextField is subclass of UIControl and to programatically add a new event you use the addTarget() method. Ex:
func a(sender: AnyObject) {}
textField.addTarget(self, action: "a:", forControlEvents: .EditingDidEndOnExit)
UIControl docs

Adjust View for keyboard appears when switching UITextField (Swift)

I have two UIView's , the Main one and one which will move up when the keyboard appears (Second UIView has two textfields). I've managed to do this with the following code:
LoginViewController.swift :
override func viewDidLoad() {
super.viewDidLoad()
self.originalCenter = self.contentView.center;
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidShow:", name: UIKeyboardDidShowNotification, object: nil)
}
func keyboardDidShow(notification: NSNotification)
{
UIView.animateWithDuration(1.0, animations: {
self.contentView.center = CGPointMake(self.originalCenter.x, self.originalCenter.y - 40);
})
}
My big problems is that because the movement of the UIView depends on the keyboard notification, when the user taps one TextField it moves up as it should, but when he taps the second one the view moves down automatically. What Im I doing wrong?
This is what is happening: Keyboard Error Gif
This question was answered over here , but I need the solution for Swift language.
Create an instance variable and check whether the keyboard was activated or not:
class ViewController: UIViewController, UITextFieldDelegate {
var _keyboardActivated: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
registerForKeyboardNotifications()
}
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self,
name: UIKeyboardDidShowNotification,
object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self,
name: UIKeyboardWillHideNotification,
object: nil)
}
func registerForKeyboardNotifications() {
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "keyboardWillShow:",
name: UIKeyboardWillShowNotification,
object: nil)
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "keyboardWillBeHidden:",
name: UIKeyboardWillHideNotification,
object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if (!_keyboardActivated) {
// do stuff
}
_keyboardActivated = true
}
func keyboardWillBeHidden(notification: NSNotification) {
_keyboardActivated = false
}
}
Set delegate of your textfield and implement the below methods
func textFieldDidBeginEditing(textField: UITextField) {
animateViewMoving(true, moveValue: 100)
}
func textFieldDidEndEditing(textField: UITextField) {
animateViewMoving(false, moveValue: 100)
}
func animateViewMoving (up:Bool, moveValue :CGFloat){
var movementDuration:NSTimeInterval = 0.3
var movement:CGFloat = ( up ? -moveValue : moveValue)
UIView.beginAnimations( "animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration )
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
UIView.commitAnimations()
}
Reference: http://www.jogendra.com/2015/01/uitextfield-move-up-when-keyboard.html

Resources