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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:14:39+00:00 2026-05-27T01:14:39+00:00

I have this code, which resizes the tableview when I click a textfield inside

  • 0

I have this code, which resizes the tableview when I click a textfield inside of a cell so that the keyboard doesn’t overlap the bottom row. The problem is, after it resizes, it scrolls to the top of the table instead of the cell that I had clicked the textfield in.

- (void)keyboardDidShow:(NSNotification *)notification
{
    NSDictionary *userInfo = [notification userInfo];
    CGSize size = [[userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    CGRect frame = CGRectMake(self.vitalsTableView.frame.origin.x, 
                              self.vitalsTableView.frame.origin.y, 
                              self.vitalsTableView.frame.size.width, 
                              self.vitalsTableView.frame.size.height - size.height);
    self.vitalsTableView.frame = frame;

    VitalsTableViewCell *cell = (VitalsTableViewCell*)[self.selectedTextField superview];
    [self.vitalsTableView scrollRectToVisible:cell.frame animated:YES];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    NSDictionary *userInfo = [notification userInfo];
    CGSize size = [[userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    self.vitalsTableView.frame = CGRectMake(self.vitalsTableView.frame.origin.x, 
                                      self.vitalsTableView.frame.origin.y, 
                                      self.vitalsTableView.frame.size.width, 
                                      self.vitalsTableView.frame.size.height + size.height);
}

- (void) textFieldDidBeginEditing:(UITextField *)textField {
    self.selectedTextField = textField;
//    [self scrollToRectOfTextField];
    NSIndexPath *path = [self.vitalsTableView indexPathForSelectedRow];
    [self.vitalsTableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

- (void)scrollToRectOfTextField {
    VitalsTableViewCell *cell = (VitalsTableViewCell*)[self.selectedTextField superview];
    CGRect r = CGRectMake(self.selectedTextField.frame.origin.x, 
                          cell.frame.origin.y+self.selectedTextField.frame.origin.y,
                          self.selectedTextField.frame.size.width,
                          self.selectedTextField.frame.size.height);
    [self.vitalsTableView scrollRectToVisible:r animated:YES];
    NSIndexPath *path = [self.vitalsTableView indexPathForSelectedRow];
    [self.vitalsTableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
  • 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-27T01:14:39+00:00Added an answer on May 27, 2026 at 1:14 am

    When you change the frame of the tableview, it calls all the necessary methods to redraw it and resets the contentOffset of the tableview to the default, which is at the top. Every time you change the frame, you will have to scroll to the CGPoint or CGRect you desire. So first save the point where you want to scroll, then resize the frame, then scroll to the point’s location relative to the new frame.

    I see in your code that you are scrolling, but it is to the wrong rect,

    - (void)scrollToRectOfTextField {
        VitalsTableViewCell *cell = (VitalsTableViewCell*)[self.selectedTextField superview];
        CGRect r = CGRectMake(self.selectedTextField.frame.origin.x, 
                              cell.frame.origin.y+self.selectedTextField.frame.origin.y,
                              self.selectedTextField.frame.size.width,
                              self.selectedTextField.frame.size.height);
        [self.vitalsTableView scrollRectToVisible:r animated:YES];
        NSIndexPath *path = [self.vitalsTableView indexPathForSelectedRow];
        [self.vitalsTableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
    

    First of all, I’d take out the last two lines about scrolling to a row at index path. Second, if you’d examine r.origin you’d probably get a point at (0, 0) because the origin of the text field’s frame is relative to its superview, not to the tableview. You can figure out where the text field’s frame is relative to the table view by using the convertRect:toView: method which is documented here and also described here.

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

Sidebar

Related Questions

I have this code which will include template.php file from inside each of these
i have this code that should create a dialog with the page google inside
I have this code which is called at an onChange event of an function
I have this code which gets WP posts, but it gets all the posts
I have this code (which is way simplified from the real code): public interface
I have this code which compiles and works as expected: class Right {}; class
I have this code which should create a splash image with either no animation
Problem Hello all! I have this code which takes my jpg image loops through
I am trying to compare two dates. I have this code which I thought
I have this code: myVariable which I want to change into trace(myVariable: + myVariable);

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.