1

I got one problem is that i wanna move my UIView(footerview) up when keyboard is showing and move it down when the keyboard is dismissed.

  • my UIView (FooterView) is contain in UIViewController in Main.Storyboard that generated automatic by Xcode.
  • I also have one TextField.

View Hierarchy will look like:

View:

-->TextField

--> UIView(FooterView)

Edit:

After I post this question I found answer by my own

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
return YES;
}


- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

[self.view endEditing:YES];
return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)keyboardDidShow:(NSNotification *)notification
{
// Assign new frame to view
[self.footerView setFrame:CGRectMake(0,250,320,65)];
}

-(void)keyboardDidHide:(NSNotification *)notification
{
// set it back to the original place
[self.footerView setFrame:CGRectMake(0,503,320,65)];
}
vichhai
  • 2,362
  • 2
  • 13
  • 23

1 Answers1

0

If you are using Autolayout you can create an IBOutlet to your UIView NSLayoutAttributeBottom constraint and change it when you need. If not, you have to move your view Frame.

For example:

- (void)keyboardWillShow:(NSNotification *)notification {
    // Get the size of the keyboard.
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    self.bottomConstraintKeyboard = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.writeView attribute:NSLayoutAttributeBottom multiplier:1 constant:keyboardSize.height];
     [self.view removeConstraint:self.bottomConstraintZero];
    [self.view addConstraint:self.bottomConstraintKeyboard];
    [UIView animateWithDuration:.5 animations:^{
        [self.view layoutIfNeeded];
    }];    
}

- (void)keyboardWillHide:(NSNotification *)notification {
    if (self.bottomConstraintKeyboard){
        [self.view removeConstraint:self.bottomConstraintKeyboard];
        self.bottomConstraintKeyboard = nil;
    }
    [self.view addConstraint:self.bottomConstraintZero];
    [UIView animateWithDuration:.5 animations:^{
        [self.view layoutIfNeeded];
    }];
}
Bisca
  • 5,381
  • 2
  • 15
  • 30
  • I'm not good with auto layout. So could u tell me any source to study on auto layout? – vichhai Apr 20 '15 at 10:10
  • You can Google It. There are a lot of examples and tutorials. For example: http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1 – Bisca Apr 20 '15 at 10:55