2014-02-05 17 views
0

我有由具有層次結構中的文本字段作爲位置的UITextField的參考其7上海華

self.view-> Tableview-> contentview-> anotherview-> myTextField將。

我想通過引用self.view來獲取MyTextField的y位置。

我的要求:

每當我點擊文本框,我打開我的鍵盤,如果文本框是我的鍵盤(我將通過比較MyTextFieldself.view參照系來檢查)下,然後我會否則會改變我的細胞的框架(如果MyTextField不在我的鍵盤下),那麼我不需要改變框架。

我想使用下面的方法,但不知道如何正確實現這一點。

- (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; 
} 
+0

檢查此[鏈接] [1]爲解決您的問題 [1]:http://stackoverflow.com/questions/11696508/how-to-convert-subviews-frame-coordinate-system -to-self-views-coordinate-syste – nik

+0

另一個有用的答案http://stackoverflow.com/questions/5407416/iphone-set-subviews-frame-outside-of-superviews-bound – nik

+0

@nik你可以點擊_help_ _Add Comment_按鈕下方的按鈕可查看註釋格式。 – Desdenova

回答

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; 
} 

簡單地做這樣的....因爲表視圖也是一個滾動視圖。然後,我學會這從堆在流只,但現在我不知道該鏈接

0

只要你設置上述鍵盤的tableview框架,你可以很容易地找到當前文本域小區表視圖,變化表視圖contentoffset對選擇的小區在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; 

} 

注:復位表格視圖中textFieldShouldEndEditing

高度
+0

將不會在ios6中工作 – Rajneesh071

+0

@ Rajneesh071在ios7中更新表格視圖單元格,試試這個UITableViewCell * cell =(UITableViewCell *)textField.superview; – NANNAV

+0

是的,我知道這一點..但你必須在你的答案中提到這一點... – Rajneesh071