これがUIです...(醜いです...)オレンジはビュー、青はテーブル、灰色はテーブルセル、最後に赤はテーブルセルのテキストフィールドです。私の質問は単純です。赤いテキストフィールドの位置を知りたいだけです...テーブルセルが上下に移動する可能性があるため、位置を変更できます。したがって、テキストフィールドの位置を動的に更新する必要はありません。ユーザーがテキストフィールドをクリックしたときの位置が必要です。アイデアはありますか?ありがとう。
5 に答える
UIView またはその派生クラスで使用できる convetrPoint メソッドを利用できます
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view
あなたの場合、これを行う必要があります:
- (void)textFieldDidBeginEditing(UITextField *)textField
{
CGPoint textFieldOriginInTableView = [textFiled convertPoint:textField.frame.origin toView:tableView];
//or if you want it relative to the orangeView, your toView should be the orangeView
}
私が言及したい2つのことがあります:
textField が tabelViewCell 内にある場合、セルがどれだけ上下しても、その位置は常に同じです。これは、textField がビューではなく tableViewCell のサブビューであるためです。
いくつかの方法で取得できます。
textFieldDidBeginEditing
委任方法について(あなたが言及したように、それが触れられたときに編集を開始することを意味します) 。または、いつでも次のコードで必要な場合:
//テキストフィールドのタグを設定できます
UITableViewCell *cell = [yourTableView cellForrowAtIndexPath:yourCellIndexPath]; UITextField *textField = (UITextField *)[cell.contentView viewWithTag:textFieldTag];
//または、タグを設定したくなく、テキストフィールドのみが存在します
for (UIView *v in [cell.contentView subviews]) { if ([v isKindOfClass:[UITextField class]]) { UITextField *textField = (UITextField *)v; } }
所有するビュー コントローラーが UITextField のデリゲートであると仮定すると、textFieldDidBeginEditing: メソッドを実装して、次のようにテキスト フィールドのフレームを取得できます。
- (void)textFieldDidBeginEditing(UITextField *)textField
{
CGRect rect = textField.frame;
}
アニメーション付きのこのコードセットフレームは、このコードを試してみてください...
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
CGRect youtTextFielfFrame = textField.frame;// get position of textfield from this code
[textField setFrame:CGRectMake(textField.frame.origin.x - 50, textField.frame.origin.y,textField.frame.size.width,textField.frame.size.height)]; /// set frame which you want here......
[UIView commitAnimations];
return YES;
}
最初に一意のタグを textField に追加します
yourTextField.tag = 1001;
textField の位置が必要な時点よりも、単に
UITextField *myTextField = [cell viewWithTag:1001];
CGRect textFieldRect = myTextField.Frame;
スーパービューに関するテキストフィールドのx、y、幅、高さパラメーターを含むタグ付きテキストフィールドのフレームを提供します。