dehlen
dehlen

Reputation: 7389

Move UIView when Keyboard appears with AutoLayout

I got an UIView which I'd like to move up/down whether the UIKeyboard appears or disappears. Unfortunately I can't get it to work. Prior to AutoLayout it was a no-brainer but since AutoLayout I have some trouble with this one.

Here is what I have so far:

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification object:nil];
    self.keyboardIsShown = NO;

}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillShowNotification
                                                  object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
}


-(void)keyboardWillShow:(NSNotification*)notification{
    if (self.keyboardIsShown) {
        return;
    }

    //change y Position of self.loginView

    self.keyboardIsShown = YES;
}

-(void)keyboardWillHide:(NSNotification*)notification{
    //Change y Position of self.loginView
    self.keyboardIsShown = NO;
}

Of course the methods get called but I need some guidance on how to change the y-Position of my UIView. Just changing the y Position of the frame does not work at all.

Here you can find my Interface Setup with the constraints I added to the UIView. Interface Setup

So all of the components except for the UIImageView are contained in the loginView Outlet.This is the view I'd like to move up or down depending on the keyboard is shown or not.

Which makes it kind of hard for me is, that ,as you might see, the UIImageView has a fixed height, width and ist centered vertically.

Upvotes: 4

Views: 7274

Answers (3)

ndnguyen
ndnguyen

Reputation: 140

What I've done so far is getting the originalFrame of the view that we want to move up and down. Make sure you get it in ViewDidAppear not ViewDidLoad

-(void)viewDidAppear:(BOOL)animated
{
    _loginHolderOriginalFrame = self.loginHolderView.frame;
}

The following function will be for show/hide keyboard

-(void) _keyboardWillShow:(NSNotification *)note
{
    [self.view layoutIfNeeded];
    [UIView animateWithDuration:0.25 animations:^{
        self.logoImageView.alpha = 0;
        CGRect rect = _loginHolderOriginalFrame; // bounds
        rect.origin.y = 15;

        [self.loginHolderView setFrame:rect];
    }];
}

-(void) _keyboardWillHide:(NSNotification *)note
{
    [self.view layoutIfNeeded];
    [UIView animateWithDuration:0.5f animations:^{
        self.logoImageView.hidden = NO;
    }];

    [self restoreLoginFrame];
}

-(void)restoreLoginFrame
{
    [UIView animateWithDuration:0.25 animations:^{
        self.logoImageView.alpha = 1;
        [self.loginHolderView setFrame:_loginHolderOriginalFrame];
    }];
}

When we show the keyboard, we move up the view to position y for e.g is equal to 15. and in case keyboard is hided, we move the view back to the original position.

Hope it help.

Upvotes: 0

Omal Perera
Omal Perera

Reputation: 3109

Move UIView when Keyboard appears (without a scrollview)


I also had a quite similar app design recently & nothing worked for me except this. I had no scrollview just a view & all the components were inside it.

- (void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}


- (void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}



- (void)keyboardWillShow:(NSNotification *)notification
{
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    [UIView animateWithDuration:0.3 animations:^{
        CGRect f = self.view.frame;
        f.origin.y = -keyboardSize.height;
        self.view.frame = f;
    }];
}

-(void)keyboardWillHide:(NSNotification *)notification
{
    [UIView animateWithDuration:0.3 animations:^{
        CGRect f = self.view.frame;
        f.origin.y = 0.0f;
        self.view.frame = f;
    }];
}

Upvotes: -1

Michael
Michael

Reputation: 1213

I've just done this. Basically, wrap everything you need to move in a container view and then animate the top constraint constant in collaboration with the keyboard show/hide notifications.

UIKeyboardWillShowNotification
UIKeyboardDidHideNotification

In the notification is a userInfo that will tell you about the height of the keyboard and also let you get your animations in sync with the keyboards for a smooth transition.

Upvotes: 4

Related Questions