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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:21:48+00:00 2026-05-22T17:21:48+00:00

I have code from Cocoa with Love that scrolls a UITextField when the keyboard

  • 0

I have code from Cocoa with Love that scrolls a UITextField when the keyboard comes up so that the keyboard does not cover the UITextField.

Here’s the code:

- (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;

    // Issue is that numerator isn't big enough for bottom 3rd of the screen

    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;

    NSLog(@"Midline: %g Fraction: %g / %g", midline, 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];
}

For debugging purposes I output the midline, etc to see what was happening. This is from tabbing from top to bottom. You can see about the 6th one down the numerator becomes negative, because the UITExtField.size.height that was converted for it’s parent view is a low number. I can’t figure out why the number would be a negative. The height should up just like all the others do as you go down the view.

2011-05-24 09:36:08.600 Baby Bloom[27794:207] Midline = 246 + 0.5 * 167

2011-05-24 09:36:08.601 Baby Bloom[27794:207] Midline: 329.5 Fraction: 22.3 / 409.6

2011-05-24 09:36:09.535 Baby Bloom[27794:207] Midline = 246 + 0.5 * 167

2011-05-24 09:36:09.536 Baby Bloom[27794:207] Midline: 329.5 Fraction: 22.3 / 409.6

2011-05-24 09:36:09.929 Baby Bloom[27794:207] Midline = 246 + 0.5 * 246

2011-05-24 09:36:09.930 Baby Bloom[27794:207] Midline: 369 Fraction: 61.8 / 409.6

2011-05-24 09:36:10.313 Baby Bloom[27794:207] Midline = 246 + 0.5 * 246

2011-05-24 09:36:10.314 Baby Bloom[27794:207] Midline: 369 Fraction: 61.8 / 409.6

2011-05-24 09:36:10.793 Baby Bloom[27794:207] Midline = 246 + 0.5 * 97

2011-05-24 09:36:10.794 Baby Bloom[27794:207] Midline: 294.5 Fraction: -12.7 / 409.6

2011-05-24 09:36:11.785 Baby Bloom[27794:207] Midline = 246 + 0.5 * 148

2011-05-24 09:36:11.786 Baby Bloom[27794:207] Midline: 320 Fraction: 12.8 / 409.6

  • 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-05-22T17:21:49+00:00Added an answer on May 22, 2026 at 5:21 pm

    Turns out that when in Landscape on the iPad it switches up the origin (x is now y and width is now height.

    Here is the code to make it work:

    - (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];
    
        float origin;
        float textFieldHeight;
        float viewOrigin;
        float viewHeight;
    
        UIInterfaceOrientation orientation =
        [[UIApplication sharedApplication] statusBarOrientation];
    
    
        if (orientation == UIInterfaceOrientationPortrait ||
            orientation == UIInterfaceOrientationPortraitUpsideDown)
        {
            textFieldHeight = textFieldRect.size.height;
            origin = textFieldRect.origin.y;
            viewOrigin = viewRect.origin.y;
            viewHeight = viewRect.size.height;
        }
        else
        {
            textFieldHeight = textFieldRect.size.width;
            viewOrigin = viewRect.origin.x;
            viewHeight = viewRect.size.width;
            origin = viewHeight - textFieldRect.origin.x;
        }
    
    
        CGFloat midline = origin + 0.5 * textFieldHeight;
    
        // Take the current middle y location of the textfield
        // If it is in the top 20% of the view don't move it
        // If it is in the middle 60% move it by a fraction of the keyboard size
        // If it is in the bottom 20% move it by the whole size of the keyboard
    
    
        CGFloat numerator = midline - viewOrigin - MINIMUM_SCROLL_FRACTION * viewHeight;
    
        CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewHeight;
    
        CGFloat heightFraction = numerator / denominator;
    
    
        if (heightFraction < 0.0)
        {
            heightFraction = 0.0;
        }
        else if (heightFraction > 1.0)
        {
            heightFraction = 1.0;
        }
    
        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;
    
        //NSLog(@"Denom: %g", denominator);
        //NSLog(@"Distance: %g", animatedDistance);
    
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
    
        [self.view setFrame:viewFrame];
    
        [UIView commitAnimations];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code examples from some of my previous work that help me to
I have a game code (from ioquake3 project) that compiles part of gameplay binaries
I have this source code from 2001 that I would like to compile. It
I have got the following code from here to read an Excel file using
I have borrowed code from this link PHP regex templating - find all occurrences
A lot of time I have to code from home. Normally I just remote
I have downloaded sample code from Apple Center.I also have gone through following question:
I have the following code: from selenium import selenium selenium = selenium(localhost, 4444, *chrome,
I have inherited some code (from zip file) from a developer and git initialzed,
Do I have to put code from .cpp in a namespace from corresponding .h

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.