ioskaveen
ioskaveen

Reputation: 196

How change the height of the UIScrollView's content when software keyboard is pushed upward

I have a view with a textview and a button like in any typical messaging app. When the keyboard is presented, the textview and button are pushed upwards as well but the content is pushed upwards off the screen where I don't see the top end of it but when I force scroll down I see it creeping down and it springs upwards when I let go. I have illustrated the problem I'm having with a quick sketch, the third screen is the desired behavior.

I have this view in a .xib file. I have a view -> ScrollView -> contentView (which has a the table and the textview and button)

enter image description here

I registered the keyboard notifications and here's the code for the show/hide methods

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

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

} 

- (void)keyboardWasShown:(NSNotification *) aNotification {
   NSDictionary *info = [aNotification userInfo];

   CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

   // the hardcoded 49 is the height of the uitabbar 
   UIEdgeInsets contentInsets = UIEdgeInsetsMake(-keyboardSize.height+49, 0.0, keyboardSize.height-49, 0.0);
   self.scrollView.contentInset = contentInsets;
   self.scrollView.scrollIndicatorInsets = contentInsets;

   [self.view addGestureRecognizer:self.tapRecognizer];

 }


- (void)keyboardWillBeHidden:(NSNotification *) aNotification {
   UIEdgeInsets contentInsets = UIEdgeInsetsZero;
   self.scrollView.contentInset = contentInsets;
   self.scrollView.scrollIndicatorInsets = contentInsets;

   [self.view removeGestureRecognizer:self.tapRecognizer];
}

Upvotes: 1

Views: 141

Answers (2)

kaushal
kaushal

Reputation: 1573

I have seen this line in keyboardWasShown method

UIEdgeInsets contentInsets = UIEdgeInsetsMake(-keyboardSize.height+49, 0.0, keyboardSize.height-49, 0.0);

Where you top Edge inset is -ve, becouse 49 - keyboardSize (which is appx 256). Replace it with this :

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height-49), 0.0);

Play with calculation best suite to you. Hope this work :)

Upvotes: 1

Gary Lip
Gary Lip

Reputation: 336

Try this.

// Called when the UIKeyboardWillShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification{

    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0.0, kbSize.height, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    CGPoint scrollPoint = CGPointMake(0, scrollView.contentSize.height - scrollView.bounds.size.height + scrollView.contentInset.bottom);
    [scrollView setContentOffset:scrollPoint animated:true];

}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification{
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}

Upvotes: 0

Related Questions