-1

I'm new to swift and I'm trying to move a UITextField up when the soft keyboard opens.

When I call view.frame.origin.y = -150 from the UIViewController, the UIView is moved but the UITextField inside it is not. (The UITextField "jumps" and animates back to its original position.)

It was working some time ago and I'm not sure what happened.

Code
  • 5,397
  • 2
  • 23
  • 68
  • You need to declare some layout constraints using NSAutoLayout to do something like that. – Luca D'Alberti Jan 19 '16 at 10:51
  • Possible duplicate of [How to make a UITextField move up when keyboard is present](http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present) – Eric Aya Jan 19 '16 at 10:52
  • Right way to handle keyboard open and close : https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html – bhavesh Jan 19 '16 at 11:11
  • @EricD. Not a duplicate. I followed that solution but it isn't working. – Code Jan 19 '16 at 11:15

2 Answers2

1

Found the problem.

I had a constraint mytextfield.Top = Top Layout Guide.Bottom, when it should be mytextfield.Top = SuperView.Bottom

Code
  • 5,397
  • 2
  • 23
  • 68
0

I think LucaD is correct - you're missing constraints between your UITextField and UIViewController.

If you are using Interface Builder, see Apple's Adding Layout Constraints by Control-Dragging

Otherwise, if you are creating the UITextField programmatically, see SWIFT | Adding constraints programmatically

Community
  • 1
  • 1
olympia
  • 332
  • 2
  • 19