-1

In objective C, this two line simple code is use to scroll the text view upwards. Is there any simple code in SWIFT like this to scroll textfield upwards?? Kindly help me.

- (void)textFieldDidBeginEditing:(UITextField *)textField {
CGPoint scrollPoint = CGPointMake(0, textField.frame.origin.y);
[scrollView setContentOffset:scrollPoint animated:YES];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
[scrollView setContentOffset:CGPointZero animated:YES];
}
Gopi Krish
  • 71
  • 1
  • 9
  • 2
    Are you saying that you can't just translate that into Swift and you want someone else to do it for you? – matt Dec 04 '14 at 15:33
  • Please clarify your question. Do you not know how to write this same code in swift, or is it not working as expected? – cmyr Dec 04 '14 at 16:45
  • Hi all! Yes, in objective c, this two line code will scroll up when keyboard appears. how to translate this code in SWIFT? – Gopi Krish Dec 06 '14 at 06:18
  • possible duplicate of [Move a view up only when the keyboard covers an input field](http://stackoverflow.com/questions/28813339/move-a-view-up-only-when-the-keyboard-covers-an-input-field) – Nerkyator Sep 24 '15 at 09:14

4 Answers4

0

on :

UITextFieldTextDidEndEditingNotification and UITextFieldTextDidBeginEditingNotification

call this method tho animate the textfield

UIView.animateWithDuration(yourTime, delay: 0, options: .CurveEaseOut, animations: {
yourTextField.frame = yourNewFrame
}, completion: { finished in
println("Basket doors opened!")
})

Hope this helps!

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0

Its better to use this:

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        scrollView.scrollEnabled = true
    yourTextfield.addTarget(self, action: "textFieldShouldReturn:", forControlEvents: .EditingDidEndOnExit)
    yourTextfield2.addTarget(self, action: "textFieldShouldReturn:", forControlEvents: .EditingDidEndOnExit)
    ...


    yourTextfield.tag = 1
    yourTextfield2.tag = 2
    ...
}   

    func textFieldShouldReturn(textField: UITextField!) -> Bool {
            let nextTag = textField.tag + 1
            let nextResponder:UIResponder? = textField.superview?.viewWithTag(nextTag)
            if ((nextResponder) != nil) {
                nextResponder?.becomeFirstResponder()
                var scrollPoint: CGPoint = CGPointMake(0, textField.frame.origin.y - 50)
                scrollView.setContentOffset(scrollPoint, animated: true)
            } else {
                textField.resignFirstResponder()
                scrollView.setContentOffset(CGPointZero, animated: true)
            }
            return false
        }
Norolim
  • 926
  • 2
  • 10
  • 25
0

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: https://stackoverflow.com/a/32755167/988169

Community
  • 1
  • 1
pkc456
  • 8,350
  • 38
  • 53
  • 109
0

you can write code in this way -

let biginSel = #selector(LoginViewController.didBeginEditing(_:))
    NSNotificationCenter.defaultCenter().addObserver(self, selector: biginSel, name: UITextFieldTextDidBeginEditingNotification, object: usrNmFld)
Prakash Raj
  • 1,943
  • 1
  • 15
  • 13