Kiwo Tew
Kiwo Tew

Reputation: 1531

Swift ios move text field when keyboard is shown

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,

Upvotes: 1

Views: 3277

Answers (2)

Gurjinder Singh
Gurjinder Singh

Reputation: 10329

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

Upvotes: 1

Teo
Teo

Reputation: 3442

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

Upvotes: 2

Related Questions