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 8515585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:11:20+00:00 2026-06-11T05:11:20+00:00

I have a question regarding the code found in the Apple documentation Moving content

  • 0

I have a question regarding the code found in the Apple documentation Moving content located under the keyboard. I have researched this, but I’m looking for clarification on the concepts I am fuzzy on here before I start getting fancy with alternate solutions.

Unfortunately, as soon as the keyboard animation finishes moving the keyboard up, the view moves down and completely out of site. This happens whether the text field is actually the one that gets covered by the keyboard or not. So now I’m back to trying to understand the code provided in the documentation and after attempts changing a variety of parameters I’ve been unable to get it to work properly.

The pertinent code provided by Apple is:

 - (void)keyboardWasShown:(NSNotification*)aNotification

NSLog(@"keyboardWasShown");
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

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

// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
    NSLog(@"if");
    CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
    NSLog(@"scrollPoint is %@", NSStringFromCGPoint(scrollPoint));
    [scrollView setContentOffset:scrollPoint animated:YES];

First of all, before I change anything as described below, here’s what I get:

GO[4614:c07] keyboardWasShown
2012-09-08 22:58:23.892 DRO GO[4614:c07] if
2012-09-08 22:58:23.892 DRO GO[4614:c07] scrollPoint is {0, -281}

So, breaking it down:

  1. NSLog just info for me indicating the method was called

  2. NSDictionary – contains info about the keyboard, including its dimensions, such that different keyboard sizes in different devices or orientations will be automatically accounted for.

  3. CGSize – obtaining the actual size from the dictionary

  4. UIEdgeInsets – found reference in UIKit Function Reference – creates an edge inset for button or view where an inset is a margin around the drawing rectangle where each side (left, right, top, and bottom) can have a different value. In this case, setting the bottom edge at the height of the keyboard. Since my view is moving down instead of up, I tried reversing the ‘top’ and ‘bottom’ values, thinking maybe they are inverted. Instead of the view scrolling smoothly down with animation, now it just blinks out of sight when the keyboard pops up and I get this:

    GO[4829:c07] if
    2012-09-08 23:06:57.146 DRO GO[4829:c07] scrollPoint is {0, -281}

  5. scrollView.contentInset: the distance that the content view is inset from the enclosing scroll view.
    Use this property to add to the scrolling area around the content. The unit of size is points. The default value isUIEdgeInsetsZero
    Seems like this should be adding area to the bottom of the scroll view to allow it to move up along with the keyboard.

  6. scrollView.scrollIndicatorInset – telling the scroll indicator to move proportionally to the view movement in response to the keyboard

  7. CGRect – creating an aRect object with the view dimensions.

  8. aRect – the height value of the CGRect, reduced by the height of the keyboard. I’m super fuzzy here – Should be the area of the view not covered by the keyboard.

  9. if – if the CGpoint is not (in the rectangle that lies under the keyboard, at a point within the active text field) – which doesn’t make sense; the logic here escapes me

  10. NSLog – telling me we got this far

  11. CGScrollPoint – defining the point the view should be scrolling to. Since view origin is upper left corner, subtracting the keyboard height should have it scroll up the distance of the keyboard height, keeping the textfield in view. Just for kicks, when I tried changing it to +kbSizeHeight I got the view disappearing swiftly and: GO[4682:c07] if
    2012-09-08 23:02:48.078 DRO GO[4682:c07] scrollPoint is {0, 679}

  12. NSLog – telling me what scroll point the view is to be moved to

  13. [scrollView – telling the scrollView to adjust to the new scrollPoing using animation

  • 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-11T05:11:21+00:00Added an answer on June 11, 2026 at 5:11 am

    I don’t know how Apple does it, I believe they recommend to put the content into a scrollview then when you tap text field, you tell the scrollview to scroll up so you can see what you’re typing.

    I am a bit of a lazy person, so all I do is tell the view itself to shift up when user taps on a field that will be hidden by the keyboard.

    I usually do this with:

    // shift view up
    self.view.transform = CGAffineTransformMakeTranslation(0, -someYOffsetValue);
    
    // shift view back down to original value
    self.view.transform = CGAffineTransformMakeTranslation(0, 0);
    

    You can wrap this in a UIView animateWithDuration: block for a smooth transition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding the following code snippet I came across in one
I have a question regarding applying a RTRIM on a ASP:Hyperlink statement. The code
I have question regarding disabling browser caching. I have already found few solutions, and
This question is regarding wordpress . I have two variables $random_n and $adcount declared
I have a question regarding construct HTML tags on back-end code (c#) in my
I know there has been several questions regarding this very question and I have
I have a question regarding Xcode. I found a tutorial on the net that
I was looking at the question being asked here, but found only answers regarding
Reading this question I found this as (note the quotation marks) code to solve
I have a question regarding core data and notifications. After reading Apple docs, I

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.