0

In my app, I have a navigation bar, a tab bar, a search bar and I always show the keypad.

However, with the different screen height with the iphone 5, I need to adjust the height of my table.

I can't anchor the table to the top of the keypad as this is shown a run time.

So I'm trying to do this dynamically, it looks like I'm missing the search bar, just too short, hmmm.

Perhaps there's an easier more obvious way?

EDIT: My code so far.

-(void)keyboardWillShow:(NSNotification*)aNotification {

int th = self.view.frame.size.height;

NSDictionary *info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] 
      CGRectValue].size;

int tableTop;
int tableHeight;

tableTop = table.frame.origin.y;

tableHeight = (th - tableTop) - kbSize.height;

[table setFrame:[General setTableBoundsByHeight:tableHeight:table]];

}

+ (CGRect)setTableBoundsByHeight:(int)lHeight:(UITableView*)tbl {

    CGRect tableFrame = tbl.frame;

    return CGRectMake(tableFrame.origin.x,
                                   tableFrame.origin.y,
                                   tableFrame.size.width,
                                    lHeight);
}
Jules
  • 7,568
  • 14
  • 102
  • 186
  • possible duplicate of [How to make a UITextField move up when keyboard is present](http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present). But please disregard the accepted answer, and make sure to read all comments. – Matthias Bauch Oct 26 '12 at 12:25
  • 1
    Please explain it better, you already have the size calculation for iPhone 4, don't you ? What is a roadblock exactly ? – A-Live Oct 26 '12 at 12:41
  • On an aside, functions like function::: are not really recommended. It is much clearer to write function: arg1: arg2: etc. – Paul de Lange Oct 26 '12 at 12:49

1 Answers1

2

I am guessing your "tableView" is part of your "self.view". If it is, it is in a different coordinate system.

The keyboard is added to the window, and tableView is on your view hierarchy. You will need to use the functions described here under the heading: Converting Between View Coordinate Systems

EDIT: If you read the documentation on UIKeyboardFrameEndUserInfoKey, you will see it mentions the rectangle is in screen coordinates, while your UITableView is almost certainly in some other views coordinate system. In the same documentation they mentioned the function to convert between the two.

Paul de Lange
  • 10,613
  • 10
  • 41
  • 56
  • He is only changing table size, not position. I can't see what could be the problem running this code on iPhone5 if the table is sized correctly w/o the keyboard. – A-Live Oct 26 '12 at 13:20
  • tableHeight = (th - tableTop) - kbSize.height; This line is mixing coordinates. The reason it works on iPhone4 is luck. – Paul de Lange Oct 26 '12 at 13:21
  • @PauldeLange I have this working now, with the code above unchanged. I was setting the height elsewhere which was causing the problem. This seems to work fine on all devices I've tried in the simulator iphone 4, 5 and none retina. So I'm concerned why you say this is mixing up the coordinates. I'd be grateful if you could explain further ? – Jules Oct 28 '12 at 09:35