获取 UITableView 滚动到选定的 UITextField 并避免被键盘隐藏
我在 UIViewController
(不是 UITableViewController
)上的表视图中有一个 UITextField
。 如果表格视图位于 UITableViewController
上,表格将自动滚动到正在编辑的 textField
以防止其被键盘隐藏。但在 UIViewController
上则不然。
我已经尝试了几天阅读多种方法来尝试实现这一目标,但我无法让它发挥作用。实际滚动的最接近的东西是:
-(void) textFieldDidBeginEditing:(UITextField *)textField {
// SUPPOSEDLY Scroll to the current text field
CGRect textFieldRect = [textField frame];
[self.wordsTableView scrollRectToVisible:textFieldRect animated:YES];
}
但是,这只会将表格滚动到最上面的行。 看似简单的任务,却经历了几天的挫折。
我正在使用以下内容来构造 tableView 单元格:
- (UITableViewCell *)tableView:(UITableView *)aTableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier = [NSString stringWithFormat: @"%d:%d", [indexPath indexAtPosition: 0], [indexPath indexAtPosition:1]];
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(180, 10, 130, 25)];
theTextField.adjustsFontSizeToFitWidth = YES;
theTextField.textColor = [UIColor redColor];
theTextField.text = [textFieldArray objectAtIndex:indexPath.row];
theTextField.keyboardType = UIKeyboardTypeDefault;
theTextField.returnKeyType = UIReturnKeyDone;
theTextField.font = [UIFont boldSystemFontOfSize:14];
theTextField.backgroundColor = [UIColor whiteColor];
theTextField.autocorrectionType = UITextAutocorrectionTypeNo;
theTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
theTextField.clearsOnBeginEditing = NO;
theTextField.textAlignment = UITextAlignmentLeft;
//theTextField.tag = 0;
theTextField.tag=indexPath.row;
theTextField.delegate = self;
theTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
[theTextField setEnabled: YES];
[cell addSubview:theTextField];
[theTextField release];
}
return cell;
}
我怀疑如果我能以某种方式在 textFieldDidBeginEditing
方法中传递 indexPath.row
,我可以让 tableView 正确滚动吗?
任何帮助表示赞赏。
I have a UITextField
in a table view on a UIViewController
(not a UITableViewController
).
If the table view is on a UITableViewController
, the table will automatically scroll to the textField
being edited to prevent it from being hidden by the keyboard. But on a UIViewController
it does not.
I have tried for a couple of days reading through multiple ways to try to accomplish this and I cannot get it to work. The closest thing that actually scrolls is:
-(void) textFieldDidBeginEditing:(UITextField *)textField {
// SUPPOSEDLY Scroll to the current text field
CGRect textFieldRect = [textField frame];
[self.wordsTableView scrollRectToVisible:textFieldRect animated:YES];
}
However this only scrolls the table to the topmost row.
What seems like an easy task has been a couple of days of frustration.
I am using the following to construct the tableView cells:
- (UITableViewCell *)tableView:(UITableView *)aTableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier = [NSString stringWithFormat: @"%d:%d", [indexPath indexAtPosition: 0], [indexPath indexAtPosition:1]];
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(180, 10, 130, 25)];
theTextField.adjustsFontSizeToFitWidth = YES;
theTextField.textColor = [UIColor redColor];
theTextField.text = [textFieldArray objectAtIndex:indexPath.row];
theTextField.keyboardType = UIKeyboardTypeDefault;
theTextField.returnKeyType = UIReturnKeyDone;
theTextField.font = [UIFont boldSystemFontOfSize:14];
theTextField.backgroundColor = [UIColor whiteColor];
theTextField.autocorrectionType = UITextAutocorrectionTypeNo;
theTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
theTextField.clearsOnBeginEditing = NO;
theTextField.textAlignment = UITextAlignmentLeft;
//theTextField.tag = 0;
theTextField.tag=indexPath.row;
theTextField.delegate = self;
theTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
[theTextField setEnabled: YES];
[cell addSubview:theTextField];
[theTextField release];
}
return cell;
}
I suspect I can get the tableView to scroll properly if I can somehow pass the indexPath.row
in the textFieldDidBeginEditing
method?
Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
在我的应用程序中,我成功地使用了
contentInset
和scrollToRowAtIndexPath
的组合,如下所示:当您想要显示键盘时,只需在表格底部添加一个 contentInset 即可所需高度:
然后,您可以安全地使用
通过添加 contentInset,即使您专注于最后一个单元格,tableView 仍然能够滚动。只需确保在关闭键盘时重置了 contentInset。
编辑:
如果您只有一个部分(可以将
cell_section
替换为 0),并使用 textView 标记来通知单元格行。In my app, I have successfully used a combination of
contentInset
andscrollToRowAtIndexPath
like this:When you want to display the keyboard, just add a contentInset at the bottom with your table with desired height:
Then, you can safely use
By adding the contentInset, even if you are focusing on the last cell the tableView will still be able to scroll. Just make sure that when you are dismissing the keyboard, you reset the contentInset.
EDIT:
If you have only one section (you can replace
cell_section
with 0) and the use the textView tag to inform the cell row.Swift
Objective C
并在 - (void)viewDidLoad
然后
Swift
Objective C
And in - (void)viewDidLoad
Then
这是对 FunkyKat 答案的调整(非常感谢 FunkyKat!)。为了未来的 iOS 兼容性,不硬编码 UIEdgeInsetsZero 可能会有好处。
相反,我询问当前的插入值并根据需要调整底部值。
This is a tweak to FunkyKat's answer (big thank you FunkyKat!). It would probably be beneficial to not hardcode UIEdgeInsetsZero for future iOS compatibility.
Instead, I ask for the current inset value and tweak the bottom value as needed.
为了其他人遇到这个问题,我在这里发布了必要的方法:
For the sake of anyone else running into this issue, I'm posting the necessary methods here:
我需要一个简单的解决方案,所以对我来说有帮助:
I needed a simple solution so for me helped:
尝试我的编码,这对 ypu 有帮助
Try my coding, this will help for ypu
苹果有一篇官方文章解释了如何像在 UITableViewController 中那样自然地做到这一点。我的 Stackoverflow 答案对此进行了解释,并提供了一个快速版本。
https://stackoverflow.com/a/31869898/1032179
Apple has an official post explaining how to do this naturally as they do in the UITableViewController. My Stackoverflow answer has this explained along with a swift version.
https://stackoverflow.com/a/31869898/1032179
您需要调整 tableView 本身的大小,使其不会位于键盘下方。
或者,您可以使用键盘通知;这工作得稍微好一些,因为你有更多的信息,并且在知道键盘何时出现方面更加一致:
然后实施:
You need to resize the tableView itself so that it does not go under the keyboard.
Alternatively, you can use a keyboard notification; this works slightly better because you have more information, and is more consistent in terms of knowing when the keyboard is coming up:
And then implement:
我的代码。也许有人会有用:
tableView
.m中的自定义文本字段单元格
My code. Maybe someone will be useful:
Custom textField cell in tableView
.m
就我而言,我的 UITableview 位于另一个 UIView 中,而 UIvie 位于主 UIScrollview 中。所以我对这类问题使用了更通用的解决方案。
我只是在特定的 UIScrollView 中找到了单元格的 Y 坐标,然后滚动到正确的点:
In my case my UITableview was inside another UIView and that UIvie was in the main UIScrollview. So I used more general solution for those kind of problems.
I simply found the Y coordinate of my cell in the specific UIScrollView and then scrolled to correct point:
另一个简单的解决方案是为最后一个表格部分的页脚添加额外的空间:
我们也可以将图标添加到该区域。 :)
Another simple solution is adding an additional space for the footer of the last table section:
We can add our icon into this area as well. :)
您可以尝试将 UITableViewController 添加到 UIViewController 而不仅仅是表视图。这样你就可以调用 UITableViewController 的 viewWillAppear 并且一切都会正常工作。
例子:
You can try adding a UITableViewController to the UIViewController instead of just a table view. This way you can call UITableViewController's viewWillAppear and everything will appear to work.
Example:
我在 @FunkyKat 和 @bmauter 答案中添加了一个小功能(顺便说一句,这是一个很好的答案,它应该是被接受的)
在键盘幻影之前/之后,保留了常规的表视图边缘插入。
I have add a little feature to @FunkyKat and @bmauter answers (great answer by the way, it should be the one accepted)
The regular Table View edge insets is preserved, before/after the keyboard apparition.