0

I have a text field that is near the bottom of the screen. When the keyboard comes up, it covers the text field. I solved this problem by using the code in iPhone Keyboard Covers UITextField.

However, that code hardcodes how much views should move up or down. Is there a way that I can programmatically obtain the coordinates of my text field so that I can determine how much it needs to move up or down? This would be beneficial because I would not have to change the code if I ever decide to move this text field to a different location. Thank you.

Community
  • 1
  • 1
abw333
  • 5,571
  • 12
  • 41
  • 48

2 Answers2

3

Use the frame property on the UITextField (or any subclass of UIView). The returned CGRectwill provide access to origin which has either the x or y coordinate as a CGFloat.

CGFloat y = textField.frame.origin.y;
Andy Obusek
  • 12,614
  • 4
  • 41
  • 62
  • excellent, that worked! thank you. i'll accept your answer as soon as I can. – abw333 Jul 01 '12 at 22:29
  • actually, I have a follow up question. why is CGFloat y = [[[textField frame] origin] y]; not working? I also tried with an * after the CGFloat. thanks! – abw333 Jul 01 '12 at 22:40
  • 1
    The origin is a CGPoint which is a struct. Structs are accessed with the `.` operator. Objective-C methods and properties can be accessed with the brackets `[]` – James Webster Jul 01 '12 at 22:49
0

The distance of the text field from the bottom of the screen should be equal or greater than the height of the keyboard.

float distanceFromBottom = txtField.window.frame.size.height - txtField.frame.origin.y - txtField.frame.size.height;
float deltaY = keyboardHeight - distanceFromBottom;

You should move the text field up by deltaY.

Hejazi
  • 16,587
  • 9
  • 52
  • 67