Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8681427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:30:28+00:00 2026-06-12T21:30:28+00:00

I got a UIViewController and I have a UIScrollView with a few UITextFields inside.

  • 0

I got a UIViewController and I have a UIScrollView with a few UITextFields inside. I got a piece of code from apple that seems to move the content from underneath the keyboard.

- (void)keyboardWasShown:(NSNotification *)notification
{
    // Step 1: Get the size of the keyboard.
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
    // Step 3: Scroll the target text field into view.
    CGRect aRect = self.view.frame;
    aRect.size.height -= keyboardSize.height;
    if (!CGRectContainsPoint(aRect, self.firstResponder.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, self.firstResponder.frame.origin.y - (keyboardSize.height-10));
        [self.scrollView setContentOffset:scrollPoint animated:YES];
    }
}

My UIScrollView is 320×416 because I have a navigation bar. Also my keyboard gets 44 more pixels because I’m adding a UIToolBar to it, so the code is not really working. I tried all kinds of configurations to solve two problems:

  1. The last two fields are being covered by the UIToolBar + keyboard but only the last one triggers the UIScrollView movement.
  2. Besides the UIScrollView is moving, the UITextField is still behind the keyboard due to the UIToolBar.

UPDATE: This works, but I’m pretty sure is wrong, and if is right, I don’t know why, doesn’t makes sense to me. Can someone fix / explain it?

- (void)keyboardWasShown:(NSNotification *)notification
{
    // Step 1: Get the size of the keyboard.
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height + 44.0f, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
    // Step 3: Scroll the target text field into view.
    CGRect aRect = self.view.frame;
   aRect.size.height -= keyboardSize.height + 44.0f;
    if (!CGRectContainsPoint(aRect, self.firstResponder.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, self.firstResponder.frame.origin.y - (keyboardSize.height-10.0f - 44.0f - 44.0f - 44.0f));
        [self.scrollView setContentOffset:scrollPoint animated:YES];
    }
}

UPDATE1: There is another bug, I have a previous button in the UIToolBar, if I tap the last text field, the scroll view goes up, If I move back to the first text view, it is outside the view, because the scroll view doesn’t rolls down.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:30:29+00:00Added an answer on June 12, 2026 at 9:30 pm

    After trying a lot to fix apple solution to the problem I ended up writing some code myself after picturing the general idea behind apple code. I think apple code over complicate the problem and doesn’t really work, by the way.

    - (void)textFieldDidBeginEditing:(UITextField*)textField {
    
        self.firstResponder = textField;
    
        if (self.firstResponder.frame.origin.y + self.firstResponder.frame.size.height > self.view.bounds.size.height - (216 + 44)) {
    
            double fix = (self.firstResponder.frame.origin.y + self.firstResponder.frame.size.height) - (self.view.bounds.size.height - (216 + 44)) + 10;
    
            CGRect rect = CGRectMake(0, -fix, self.view.frame.size.width, self.view.frame.size.height);
    
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.3];
            self.view.frame = rect;
            [UIView commitAnimations];
    
        } else if (self.firstResponder.frame.origin.y + 10 < -self.view.frame.origin.y) {
    
            double fix =  -self.view.frame.origin.y - (-self.view.frame.origin.y - self.firstResponder.frame.origin.y) - 10;
    
            CGRect rect = CGRectMake(0, -fix, self.view.frame.size.width, self.view.frame.size.height);
    
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.3];
            self.view.frame = rect;
            [UIView commitAnimations];
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got a memory bug that seems to boil down to something happening
I want to push UIViewController from UIView. I have searched for that and i
Got a seg fault from my memcpy that gdb can't give me anything else
I've got a fairly visually complex app that has a base UIViewController and several
I've got quite a problem with UIScrollView - I have a SwitchViewController (which is
I have a view controller (derived from UIViewController not UITableViewController) containing a table view.
I have a problem with this code: Header: @interface ViewController : UIViewController { IBOutlet
I have the following problem: I got a UIViewController and it contains a textfield
I've got a serious doubt. Suppose the following scenario: You have a UIViewController onscreen.
I have a very simple UIScrollView example that simply doesn't do what it's supposed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.