user2810114
user2810114

Reputation: 175

IOS : View height is getting reduced when performing animation

I have the following code for moving the view up and down when keyboard appears on a text field.

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"height before animation%f",self.view.frame.size.height);
    NSLog(@"%f",self.view.frame.size.height);
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [self.view setFrame:CGRectMake(0,-216,320,460)];
    [UIView commitAnimations];
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [self.view setFrame:CGRectMake(0,0,320,460)];
    [UIView commitAnimations];
    NSLog(@"height after animation %f",self.view.frame.size.height);
    }

Here's a sample log I get when keyboard appears and then editing is finished :

2014-01-21 11:00:51.194 Master-view[456:70b] height before animation 568.000000
2014-01-21 11:00:53.635 Master-view[456:70b] height after animation 460.000000

The height of the view seems to get reduced which is making bottom part of the screen non-interactable. Why is this happening ?

View is moving up and is coming down too without issue. Also visually there appears to be no difference. All elements which were there before moving up are there after coming down. But the elements at bottom of the screen ( beyond the height of 460.0) are not intractable.

Upvotes: 0

Views: 223

Answers (3)

sarit bahuguna
sarit bahuguna

Reputation: 875

We use NSNotification keyboard will show and keyboard will hide for that.

we use add notification for keyboard show and hide we add in viewWillAppear a notification and remove Notification in viewWillDisappear.

-(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 {

    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}

-(void)keyboardWillHide {
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}



-(void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; 

    CGRect rect = self.view.frame;
    if (movedUp)
    {

        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect.size.height += kOFFSET_FOR_KEYBOARD;
    }
    else
    {
        rect.origin.y += kOFFSET_FOR_KEYBOARD;
        rect.size.height -= kOFFSET_FOR_KEYBOARD;
    }
    self.view.frame = rect;

    [UIView commitAnimations];
}

Upvotes: 0

kamalesh kumar yadav
kamalesh kumar yadav

Reputation: 966

 -(void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"height before animation%f",self.view.frame.size.height);
NSLog(@"%f",self.view.frame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[self.view setFrame:CGRectMake(0,-216,320,self.view.frame.size.height)];
[UIView commitAnimations];

}

-(void)textFieldDidEndEditing:(UITextField *)textField{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[self.view setFrame:CGRectMake(0,0,320,self.view.frame.size.height)];
[UIView commitAnimations];
NSLog(@"height after animation %f",self.view.frame.size.height);
}

I set the frame as per view height you only change the y position when key beginediting and try to set it back when end editing may this help you

Upvotes: 6

Akhilrajtr
Akhilrajtr

Reputation: 5182

Try changing like

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"height before animation%f",self.view.frame.size.height);
    NSLog(@"%f",self.view.frame.size.height);
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y = -216;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [self.view setFrame:viewFrame];
    [UIView commitAnimations];
}

-(void)textFieldDidEndEditing:(UITextField *)textField{

     CGRect viewFrame = self.view.frame;
     viewFrame.origin.y = 0;
     [UIView beginAnimations:nil context:NULL];
     [UIView setAnimationDuration:0.3];
     [self.view setFrame:viewFrame];
     [UIView commitAnimations];
     NSLog(@"height after animation %f",self.view.frame.size.height);
}

Upvotes: 0

Related Questions