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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:25:07+00:00 2026-05-29T09:25:07+00:00

Right now, I have basic code for moving the textfield above the keyboard when

  • 0

Right now, I have basic code for moving the textfield above the keyboard when you start editing. However, the size of the textfield varies based on device and orientation. So, I wrote a crude way of doing it, which doesn’t stay consistently right above the keyboard, but instead will go up further when you rotate it, and so it doesn’t look as professional as I would like.

The basic sense of my question is if there is a logic for getting the size of the keyboard based on device and orientation and using that value automatically and hopefully faster than this.

If that is the best way, please let me know. Otherwise, please provide input. Here is the code that I have.
(This is just the move-up code, not the move down code, in order to prevent taking up too much space)

- (void)textFieldDidBeginEditing:(UITextField *)textField { 

    //Get Device Type
    NSString *deviceType = [[UIDevice currentDevice] model];

    //Animate Text Field
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.4];
    [UIView setAnimationBeginsFromCurrentState:YES];

    if ([deviceType isEqualToString:@"iPhone"]) {

        //Size For iPhone
        googleBar.frame = CGRectMake(googleBar.frame.origin.x - 62.0, (googleBar.frame.origin.y - 210.0), googleBar.frame.size.width + 120.0, googleBar.frame.size.height);

    } else if ([deviceType isEqualToString:@"iPad"]) {

        //Size for iPad
        googleBar.frame = CGRectMake(googleBar.frame.origin.x - 62.0, (googleBar.frame.origin.y - 320.0), googleBar.frame.size.width + 120.0, googleBar.frame.size.height);

    } else if ([deviceType isEqualToString:@"iPod touch"]) {

        //Size For iPod Touch
        googleBar.frame = CGRectMake(googleBar.frame.origin.x - 62.0, (googleBar.frame.origin.y - 210.0), googleBar.frame.size.width + 120.0, googleBar.frame.size.height);

    } 

    [UIView commitAnimations];

} 
  • 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-29T09:25:08+00:00Added an answer on May 29, 2026 at 9:25 am

    What you really want to do is observe the UIKeyboard(Did|Will)(Show|Hide) notifications. They contain in their userInfo dictionaries the beginning and ending frame, as well as the correct animation curve and durations.

    So after observing this notification, when it’s posted move your text field based on the size of the frame passed in the notification, according to the animation hints provided.

    You can see more information in the UIWindow class reference’s “notifications” section: https://developer.apple.com/library/ios/#documentation/uikit/reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html

    Below is a sample view controller implementation. The nib for this view controller was just a single text field, with an outlet connected to it, and the text field’s delegate set to the view controller.

    @interface ViewController ()
    
    - (void)viewControllerInit;
    
    @end
    
    @implementation ViewController
    
    @synthesize textField;
    
    - (id)initWithCoder:(NSCoder *)coder {
        self = [super initWithCoder:coder];
        if (self) {
            [self viewControllerInit];
        }
        return self;
    }
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
        {
            [self viewControllerInit];
        }
        return self;
    }
    
    
    - (void)viewControllerInit
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    
    
    - (void)dealloc {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    
    #pragma mark - Notification Handlers
    
    - (void)keyboardWillShow:(NSNotification *)notification
    {
        // I'll try to make my text field 20 pixels above the top of the keyboard
        // To do this first we need to find out where the keyboard will be.
    
        NSValue *keyboardEndFrameValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect keyboardEndFrame = [keyboardEndFrameValue CGRectValue];
    
        // When we move the textField up, we want to match the animation duration and curve that
        // the keyboard displays. So we get those values out now
    
        NSNumber *animationDurationNumber = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
        NSTimeInterval animationDuration = [animationDurationNumber doubleValue];
    
        NSNumber *animationCurveNumber = [[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey];
        UIViewAnimationCurve animationCurve = [animationCurveNumber intValue];
    
        // UIView's block-based animation methods anticipate not a UIVieAnimationCurve but a UIViewAnimationOptions.
        // We shift it according to the docs to get this curve.
    
        UIViewAnimationOptions animationOptions = animationCurve << 16;
    
    
        // Now we set up our animation block.
        [UIView animateWithDuration:animationDuration 
                              delay:0.0 
                            options:animationOptions 
                         animations:^{
                             // Now we just animate the text field up an amount according to the keyboard's height,
                             // as we mentioned above.
                            CGRect textFieldFrame = self.textField.frame;
                            textFieldFrame.origin.y = keyboardEndFrame.origin.y - textFieldFrame.size.height - 40; //I don't think the keyboard takes into account the status bar
                            self.textField.frame = textFieldFrame;
                         } 
                         completion:^(BOOL finished) {}];
    
    }
    
    
    - (void)keyboardWillHide:(NSNotification *)notification
    {
    
        NSNumber *animationDurationNumber = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
        NSTimeInterval animationDuration = [animationDurationNumber doubleValue];
    
        NSNumber *animationCurveNumber = [[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey];
        UIViewAnimationCurve animationCurve = [animationCurveNumber intValue];
        UIViewAnimationOptions animationOptions = animationCurve << 16;
    
        [UIView animateWithDuration:animationDuration 
                              delay:0.0 
                            options:animationOptions 
                         animations:^{
                             self.textField.frame = CGRectMake(20, 409, 280, 31); //just some hard coded value
                         } 
                         completion:^(BOOL finished) {}];
    
    }
    #pragma mark - View lifecycle
    
    - (void)viewDidUnload
    {
        [self setTextField:nil];
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    #pragma mark - UITextFieldDelegate
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [self.textField resignFirstResponder];
        return YES;
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET app, very basic, but right now too much code to
Good day! I right now have a function the drags an element from a
Right now I have a database (about 2-3 GB) in PostgreSQL, which serves as
Right now I have an SSIS package that runs every morning and gives me
Right now I have the following in my .vimrc : au BufWritePost *.c,*.cpp,*.h !ctags
Right now I have a few new applications being developed against an Oracle Database,
Right now I have a JSP page that allows to sort some items, when
Right now I have a table called Campaigns that has many Hits, if I
Right now I have this SQL query which is valid but always times out:
Right now I have def min(array,starting,ending) minimum = starting for i in starting+1 ..ending

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.