Erik
Erik

Reputation: 1254

iOS ViewController subview moving when keyboard is presented

I have a UITableView which is a property of my view controller. This view controller is positioned 40 pixels below the top of the screen using autolayout + storyboard constraints. I recently added a label and textfield that are initially hidden in storyboard and constrained to be directly behind the tableview.

I added a method to display these fields:

-(void)showNeighborhoodFilter { [UIView animateWithDuration:.5 animations:^{ if (self.neighborhoodLabel.hidden) { self.neighborhoodLabel.hidden=NO; self.neighborhoodSelector.hidden=NO; self.tableView.frame = CGRectMake(0, self.tableView.frame.origin.y+40, self.tableView.frame.size.width, self.tableView.frame.size.height-40); } else{ self.neighborhoodLabel.hidden=YES; self.neighborhoodSelector.hidden=YES; self.tableView.frame = CGRectMake(0, self.tableView.frame.origin.y-40, self.tableView.frame.size.width, self.tableView.frame.size.height+40); } }]; }

This seems to work fine except when I tap the textfield, it moves the tableview back up above the hidden textfield. I am at a loss for what's going on. First StateSecond StateThird State

Upvotes: 2

Views: 249

Answers (2)

HDdeveloper
HDdeveloper

Reputation: 4404

For changing text filed height declare NSLayoutConstraint variable constraint in your controller.

In .h file:

    __weak IBOutlet NSLayoutConstraint *heightConstraintOfMyView;//height of the view
    __weak IBOutlet NSLayoutConstraint *verticalspaceConstraintOfMyViewWithTopView;//space between myview and the view just above it
    __weak IBOutlet NSLayoutConstraint *verticalSpaceConstraintOfMyViewWithBottomView;//space between myview and the view just below it

In your storyboard enter image description here

Connected

Link variables

Vary the frame using constraints.

if (myConditionTrueForShowingView) {
        //setting the constraint to update height 
        heightConstraintOfMyView = 40;
        verticalspaceConstraintOfMyViewWithTopView.constant=20;
        verticalSpaceConstraintOfMyViewWithBottomView.constant=80;
     }
  else{
        //setting the constraint to update height 
        heightConstraintOfMyView = 0;
        verticalspaceConstraintOfMyViewWithTopView.constant=20;
        verticalSpaceConstraintOfMyViewWithBottomView.constant=0;
     }

You can also register for keyboard notifications.

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];

}

And Handle the height changes in the notifications call back-

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

-(void)keyboardWasShown:(NSNotification*)aNotification
{
    self.view.translatesAutoresizingMaskIntoConstraints = YES;
    if (self.tableBottomSpaceConstrain.constant) {//
        return;
    }
    self.tableBottomSpaceConstrain.constant = 216;// Default Keyboard height is 216, varies for third party keyboard
    [self.view setNeedsUpdateConstraints];
    [self.view updateConstraints];
    [self.view layoutIfNeeded];
    [self.view setNeedsLayout];

    [self.view setNeedsUpdateConstraints];
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    self.tableBottomSpaceConstrain.constant =0;
    [self.view layoutIfNeeded];
    [self.view setNeedsLayout];
    [self.view updateConstraints];
}

Learn more about NSLayoutConstraint Implementing AutoLayout Constraints in Code

See Apple Doc

You can use some third party code if textfield is part of tableviewTPKeyboardAvoiding

See this link: How to make a UITextField move up when keyboard is present?

Making a UITableView scroll when text field is selected

Upvotes: 1

jcaron
jcaron

Reputation: 17720

If you use auto-layout, you cannot move a view that has constraints, it will be moved again any time iOS decides to recompute the constraints.

You need to either change the constraints (you can change the constant), or enable/disable constraints.

Upvotes: 0

Related Questions