Move the UIView when a keyboard with AutoLayout appears

I have UIViewone that I would like to move up / down, appears or disappears UIKeyboard. Unfortunately, I cannot get it to work. Before AutoLayoutit was not easy, but since AutoLayoutI have some problems with this.

Here is what I still have:

- (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 are called, but I need some guidance on changing my y-position UIView. Simply changing the y position of the frame does not work at all.

Here you can find my interface setup with the restrictions that I added in the UIView. Interface setup

Therefore, all components, except UIImageView, are contained in loginView Outlet. This is a view that I would like to move up or down depending on the keyboard, displayed or not.

, , , UIImageView , .

+4
3

. , , , , show/hide .

UIKeyboardWillShowNotification
UIKeyboardDidHideNotification

, , .

+4

, , - , . , ViewDidAppear, ViewDidLoad

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

/

-(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];
    }];
}

, y, , 15. , , .

, .

0

UIView, ( )


, , . scrollview , .

- (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;
    }];
}
-1

All Articles