1

I am new to ios/swift and not sure what to do when it comes to text fields / keyboard.

When I click on a textfield the keyboard blocks/covers it, so I cant select it or any other text field bellow it.

So what is the best solution for this? Beside wrapping everything in a scrollview.

I found this snippet but im not sure on how to implement it:

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()
    }

Or if someone has a good code example/library from github, please share :)

Thanks in advance,

Juan Catalan
  • 2,299
  • 1
  • 17
  • 23
Kiwo Tew
  • 1,491
  • 1
  • 22
  • 39
  • Take a look to [this answer](http://stackoverflow.com/questions/28813339/move-a-view-up-only-when-the-keyboard-covers-an-input-field/28813720#28813720), it should help you – Nerkyator Jun 05 '15 at 13:19
  • Follow this tutorial..http://effortlesscode.com/auto-layout-keyboard-shown-hidden/ – Mohammad Zaid Pathan Jun 05 '15 at 13:26

2 Answers2

2

First of all, you should register for two keyboard notifications:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)

Then, you should embed your text fields in a UIView (let's call it inputView). Next, you should take a reference to the top (or bottom constraint) of the view. Here's the example with the bottom constraint:

@IBOutlet weak var inputViewBottomConstraint: NSLayoutConstraint!

Next, implement the two observer functions:

func keyboardWillShow(notification : NSNotification) {

    var keyboardSize = notification.userInfo?[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size

    previousConstant = self.inputViewBottomConstraint.constant
    self.inputViewBottomConstraint.constant = keyboardSize!.height

    UIView.animateWithDuration(0.3, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
        self.layoutIfNeeded()
    })
}

func keyboardWillHide(notification : NSNotification) {

    self.inputViewBottomConstraint.constant = previousConstant
    self.layoutIfNeeded()
}
Teo
  • 3,394
  • 11
  • 43
  • 73
1

Swift 3.0 - If your textField inside UITableView

    NotificationCenter.default.addObserver(self, selector:#selector(youViewContorllerName.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector:#selector(youViewContorllerName.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

  func keyboardWillShow(notification : NSNotification) {

        let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue.size

        self.constraintTblBottom.constant = keyboardSize.height
        UIView.animate(withDuration: 0.3, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: { () -> Void in
            self.layoutIfNeeded()
        })
    }

    func keyboardWillHide(notification : NSNotification) {
        self.constraintTblBottom.constant = 0
        self.layoutIfNeeded()
    }
Gurjinder Singh
  • 9,221
  • 1
  • 66
  • 58