0

i've got a UIView, dragged of Object Library, inside of a UIViewcontroller created by StoryBoard.

Inside of this UIView, I put two Text Field to grab user name and password, and one button to log in.

How can I move only this View when the keyboard appears? I'm able to move the whole View Controller, but i can't find a solution to move only the View.

I just dragged one View of the Object Library, inside my Viewcontroller and created one Outlet in .h Viecontroller - @property (weak, nonatomic) IBOutlet UIView *contentView; and connected do this View, i'm in doubt too if I need to do something more to add the view to the project.

Thanks in advance!!

View inside UIViewController;

Edward
  • 61
  • 1
  • 10
  • Not sure if I should mark as a duplicate of http://stackoverflow.com/questions/11282449/move-uiview-up-when-the-keyboard-appears-in-ios – danh May 19 '15 at 14:42

1 Answers1

2

You have to subscribe for UIKeyboardWillShowNotification and UIKeyboardWillHideNotification. Then move modal view up when keyboard appears, down when keyboard hides.

Then animate modal view up or down whether keyboard will show or hide:

Move up:

[UIView animateWithDuration:ANIMATION_DURATION animations:^{
    CGRect currentFrame = modalView.frame;
    currentFrame.origin.y -= VIEW_FRAME_OFFSET;
    modalView.frame = currentFrame;
}];

Move down:

[UIView animateWithDuration:ANIMATION_DURATION animations:^{
    CGRect currentFrame = modalView.frame;
    currentFrame.origin.y += VIEW_FRAME_OFFSET;
    modalView.frame = currentFrame;
}];

Also if you want you can use other keyboard notifications listed here.

Dima Cheverda
  • 402
  • 1
  • 4
  • 10
  • The Uiview only moves quickly after implement this code and returns to the same point, beneath the keyboard. Any ideia? Thanks! – Edward May 19 '15 at 21:30