Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>set delegate of your scrollView as self whereever you are crating scrollview in your code</p> <pre><code> ((UIScrollView*)self.view).delegate=self; </code></pre> <p>then </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; ((UIScrollView*)self.view).scrollEnabled=YES; } } - (void) keyboardWillHide:(NSNotification *)notification { UIEdgeInsets contentInsets = UIEdgeInsetsZero; ((UIScrollView*)self.view).contentInset = contentInsets; ((UIScrollView*)self.view).scrollIndicatorInsets = contentInsets; [((UIScrollView*)self.view) setContentOffset:CGPointMake(0.0f, 0.0f) animated:TRUE]; ((UIScrollView*)self.view).contentSize = CGSizeMake(((UIScrollView*)self.view).contentSize.width, ((UIScrollView*)self.view).contentSize.height); ((UIScrollView*)self.view).scrollEnabled=NO; } #pragma mark - set scrollView content position -(void)scrollViewToCenterOfScreen:(UIView *)theView { CGFloat theViewY = theView.center.y; CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; CGFloat avaliableHeight = applicationFrame.size.height - 300; CGFloat y = theViewY - avaliableHeight / 2.0; if(y&lt;0) y = 0; [((UIScrollView*)self.view) setContentOffset:CGPointMake(0,y) animated:YES]; } #pragma -mark UITextField Delegate methods - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { [self scrollViewToCenterOfScreen:textField]; return YES; } </code></pre> <p>Now set delegate of your text field. Whenever textFieldShouldBeginEditing: your scrollview automatically move on that. scrollViewToCenterOfScreen: method set your scrollview position at textfiled position.</p> <p>It definitely set your scrollview content insets as well as setContentOffset. Please let me know if you still facing this problem. Thanks</p>
 

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