0

I am having a textfield which is having the hierarchy as

self.view->Tableview->contentview->anotherview->MyTextfield.

I want to get y position of MyTextField with reference of self.view.

My requirement:

Whenever I click on textfield, I am opening my keyboard, if the textfield is under my keyboard (which I will be checking by comparing the frame of MyTextField with reference of self.view) then I will be changing the frame of my cell otherwise (if MyTextField is not under my keyboard) then I dont need to change the frame.

I am trying to use the following method, but dont know how to exactly implement this.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{

    UIView *aView = textField.superview.superview.superview.superview.superview.superview.superview;
    NSLog(@"%@",[aView class]);
    CGRect textFieldRect = [textField  convertRect:textField.frame fromView:aView];
    NSLog(@"View: %@",NSStringFromCGRect(textFieldRect));
    return YES;
} 
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Rahul Mathur
  • 872
  • 1
  • 7
  • 20
  • Check this [link][1] for solution to your problem [1]: http://stackoverflow.com/questions/11696508/how-to-convert-subviews-frame-coordinate-system-to-self-views-coordinate-syste – nik Feb 05 '14 at 13:22
  • And another useful answer http://stackoverflow.com/questions/5407416/iphone-set-subviews-frame-outside-of-superviews-bound – nik Feb 05 '14 at 13:24
  • @nik You can click the _help_ button beneath the _Add Comment_ button to see the comment formatting. – Desdenova Feb 05 '14 at 13:27
  • 1
    You should not use things like `superview.superview.superview.superview.superview.superview.superview`. They might look funny, but they will break easily. Your code won't work on iOS6 because Apple added a new view between the cell and its contentView in iOS7. And Apple might do that again at any time. You must not rely on the view hierarchy of views that you did not create (as in subclassed UIView) yourself. – Matthias Bauch Feb 05 '14 at 13:28
  • @Matthias Bahuch I know the iOS hierarchy, but at the time of doing this i was checking the class type of my aView which was shown as UIView. :) – Rahul Mathur Feb 06 '14 at 06:37

2 Answers2

0
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    CGPoint pt;
    CGRect rc = [textField bounds];
    rc = [textField convertRect:rc toView:tableView];
    pt = rc.origin;
    pt.x = 0;
    pt.y -= (165 + ([UI isIPhone5] ? 80 : 0));
    [tableView setContentOffset:pt animated:YES];

  return YES;
}

simply do like this....Because table view also a scrollview. Then i learn this from stack over flow only but now I dont know the link

TamilKing
  • 1,643
  • 1
  • 12
  • 23
0

Simply you set tableview frame above keyBoard, you can easily find current textField cell in table view,change table view contentoffset for selected cell in textFieldShouldBeginEditing

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
  // set table view frame
  UITableViewCell *cell = (UITableViewCell *)textField.superview.superview.superview;
  NSIndexPath *indexPath = [self.filterTableView indexPathForCell:cell];
  [tableView scrollToRowAtIndexPath:selectedIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
 return YES;

}

NOTE: Reset table view height in textFieldShouldEndEditing

NANNAV
  • 4,875
  • 4
  • 32
  • 50
  • @Rajneesh071 table view cell updated in ios7, try this UITableViewCell *cell = (UITableViewCell *)textField.superview; – NANNAV Aug 30 '14 at 06:15