How to resize views when the keyboard appears using auto-layout

I have a view (a UITableView in this case, but it doesn’t matter) on my UIViewController that I want the height to be changed when the keyboard pops up.

What is the best way to do this in Auto Layout? I currently have these limitations:

  • Top viewing space: 0
  • Trailing space to view: 0
  • Leading space for observation: 0
  • Bottom view space: 0
  • Height is 424

I think the fastest way to do this is to remove the height and bottom space limit and just resize the actual view in the code when the keyboardDidAppear notification is called, but are there other ways to do this?

EDIT: I removed the height limit, mine is bad.

+7
ios uitableview autolayout interface-builder
source share
3 answers

I would give you a general idea, it may need to be adjusted for your actual project.

I believe that the UITableView *_tableView was correctly set up somewhere earlier.

 - (void)viewDidLoad { // ... [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:nil usingBlock:^(NSNotification *note) { id _obj = [note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey]; CGRect _keyboardFrame = CGRectNull; if ([_obj respondsToSelector:@selector(getValue:)]) [_obj getValue:&_keyboardFrame]; [UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{ [_tableView setContentInset:UIEdgeInsetsMake(0.f, 0.f, _keyboardFrame.size.height, 0.f)]; } completion:nil]; }]; [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:nil queue:nil usingBlock:^(NSNotification *note) { [UIView animateWithDuration:0.25f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{ [_tableView setContentInset:UIEdgeInsetsZero]; } completion:nil]; }]; // ... 

}

NOTE: if your UITTableView not at the bottom of the screen for sure, the value of the contentInset should be specified on this line: [_tableView setContentInset:UIEdgeInsetsMake(0.f, 0.f, _keyboardFrame.size.height, 0.f)];

+16
source share

Keep the height limit and connect the output to it:

 @property (weak, nonatomic) IBOutlet NSLayoutConstraint *CS_TableView_Height; 

Take a look at Example 3 of this tutorial .

Update the height of your view by NSNotificationCenter keyboard event using NSNotificationCenter :

 - (void)viewWillAppear:(BOOL)animated { // register for keyboard notifications [[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 { // unregister for keyboard notifications while not visible. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } 

You might also want to take a look at the accepted answer to this question for some inspiration.

So in the end you should get something like this:

 - (void)keyboardWillShow { self.CS_TableView_Height.constant = 500;//For example [self.tableView setNeedsUpdateConstraints]; } - (void)keyboardWillHide { self.CS_TableView_Height.constant = 568;// iPhone5 height [self.tableView setNeedsUpdateConstraints]; } 
+2
source share

First, you should not add a top to bottom constraint and height constraint. If the screen size changes, the application will crash (if one of the restrictions does not have a lower priority, in which case it will be deleted).

Secondly, in your keyboardDidAppear notification method, you simply change the lower space for the value of the observation constant and call [myView setNeedsDisplay]

Edit: you are not doing setNeedsDisplay after it became the first. You add self as an observer for keyboardWillShow / keyboardWillHide notifications, and in this method you update the constraint and call setNeedsDisplay .

See this message in Apple , Listing 5-1 Handling the keyboard notifications and Listing 5-2 Additional methods for tracking the active text field specify the code.

0
source share

All Articles