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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:59:09+00:00 2026-06-12T18:59:09+00:00

I currently use this method to scroll the view up and down when I

  • 0

I currently use this method to scroll the view up and down when I have multiple text fields on the screen and don’t want the keyboard hiding the text field. I found this code online and it works very well for the most part.

In the interface file

@interface ViewController : UIViewController<UITextFieldDelegate>
{

    IBOutlet UITextField *textField1;
    IBOutlet UITextField *textField2;
    IBOutlet UITextField *textField3;

    //Float
    CGFloat animatedDistance;

}

in the implementation

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;

Then I use these methods to make the view scroll up and down when text field is clicked

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
    CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];

    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
    CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
    CGFloat heightFraction = numerator / denominator;

    if (heightFraction < 0.0)
    {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }

    UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
    }
    else
    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
    }

    CGRect viewFrame = self.view.frame;

    viewFrame.origin.y -= animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];

}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y += animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
}

In this example, I have 3 text fields, positioned at top of the screen, middle of the screen and bottom of the screen. I also have this tool bar added to each text field with next previous and done buttons in it. I am sure you can guess what they do.

This code works well except for one issue. If I am editing a text field, and I click on a text field in the middle of the screen, the view scrolls up so the text field isn’t hidden, which is good. However if I then click the home button while in the middle of editing, then access the application again, when the view is opened again, the scroll is reset back to way it normally is, but the textfield is still being edited, so when I do the click the done button, the view acts is if it is still scrolled up, so it scrolls back down again, which causes the view to scroll off the bottom of the screen

I have also tried using notifications for when keyboard is shown or hidden to scroll the view, but same problem happens.

Has anyone had this issue before? And if so, how did they resolve it.

Thanks in advance

  • 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-12T18:59:11+00:00Added an answer on June 12, 2026 at 6:59 pm

    Even i got this issue, and fixed like below.

    You need to add an observer for UIApplicationWillResignActiveNotification in your ViewController where textFields are present.

    And for that action resign all the keyboards (resignFirstResponder).

    This makes your view in right position before app entering background.

    And make sure add & remove the notification observers in the right place.

    You can add observer in viewDidLoad and remove observer in viewDidUnLoad.

    Their might be one more issue when using this view scroll with textFields, if the orientation is supported this issue can occur.

    Let me know if you tracked this issue, will help you.

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

Sidebar

Related Questions

I have a working website, where I use this expression everywhere. price.ToString(C) It currently
I currently use this handy conversion extension method to do conversions between types: public
I currently use this method to encrypt the AppSettings section of my application's web.config
With reference to this programming game I am currently building. Important: Scroll down to
I am currently trying to use this gem http://github.com/pengwynn/linkedin . Everything works fine (i.e.
This is the code I currently use: <% Uri MyUrl = Request.UrlReferrer; if( MyUrl
I currently use, base64_encode() to encode a user's password, this works well because it
Basically, this is what I currently use in an included file: $sites[0]['url'] = http://example0.com;
I'm a beginner coder in Ruby and currently I'm trying to use this plugin:
This concerns the state of Struts2 framework tooling, although currently I use no IDE

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.