Note that there are some explanatory texts on larger screens.

plurals
  1. POUIScrollView Content Insets not working for Keyboard Height
    text
    copied!<p>I am trying to move a <code>UIScrollView</code> when the keyboard hides a <code>UITextField</code> by changing the size using the <code>contentInsets</code> as it is shown. </p> <p>However, it's not working for the keyboard height. The keyboard height comes as 216, but it only stops scrolling at the correct location if I set the bottom inset to 515 for iPhone portrait mode and 310 for iPhone landscape mode. Why would these dimensions be so different? I don't want to hardcode these arbitrary values in.</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; self.view.frame = self.parentViewController.view.frame; [self.textView becomeFirstResponder]; NSLog(@"scrollview: %f,%f, parent: %f,%f", self.view.frame.size.height, self.view.frame.size.width, self.parentViewController.view.frame.size.height, self.parentViewController.view.frame.size.width); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } - (void)keyboardWasShown:(NSNotification *)notification { if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone) { // Step 1: Get the size of the keyboard. CGFloat keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height; // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height. UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0); ((UIScrollView*)self.view).contentInset = contentInsets; ((UIScrollView*)self.view).scrollIndicatorInsets = contentInsets; // Step 3: Scroll the target text field into view. CGRect aRect = self.view.frame; aRect.size.height = aRect.size.height - keyboardHeight; if (!CGRectContainsPoint(aRect, self.textView.frame.origin) ) { CGPoint scrollPoint = CGPointMake(0.0, self.textView.frame.origin.y - keyboardHeight); [((UIScrollView*)self.view) setContentOffset:scrollPoint animated:YES]; } } } - (void) keyboardWillHide:(NSNotification *)notification { UIEdgeInsets contentInsets = UIEdgeInsetsZero; ((UIScrollView*)self.view).contentInset = contentInsets; ((UIScrollView*)self.view).scrollIndicatorInsets = contentInsets; } </code></pre> <p>Edit: </p> <p>Before the keyboard is open, i print this out:</p> <pre><code>NSLog(@"scrollview: %f,%f, parent: %f,%f", self.view.frame.size.height, self.view.frame.size.width, self.parentViewController.view.frame.size.height, self.parentViewController.view.frame.size.width); </code></pre> <p>and it prints this:</p> <pre><code>scrollview: 431.000000,320.000000, parent: 431.000000,320.000000 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload