也许这是一个反复出现的问题,但我被这个问题和一些 iOS 概念所困扰。我有一个带有静态表视图、三个部分和每个部分的一些行的ViewController 。在行内我有 UITextFields。我想要做的是阻止键盘隐藏我的底部屏幕 UI 文本字段。我刚刚尝试了管理键盘中的苹果解决方案,但是由于我没有将滚动视图背后的概念附加到静态表格视图中,因此我无法将这个想法实施到我的项目中。大家有没有推荐的地方学习呢?抱歉,如果无法解释我要做什么。我有点失落。
任何帮助将不胜感激。
非常感谢,马科斯。
也许这是一个反复出现的问题,但我被这个问题和一些 iOS 概念所困扰。我有一个带有静态表视图、三个部分和每个部分的一些行的ViewController 。在行内我有 UITextFields。我想要做的是阻止键盘隐藏我的底部屏幕 UI 文本字段。我刚刚尝试了管理键盘中的苹果解决方案,但是由于我没有将滚动视图背后的概念附加到静态表格视图中,因此我无法将这个想法实施到我的项目中。大家有没有推荐的地方学习呢?抱歉,如果无法解释我要做什么。我有点失落。
任何帮助将不胜感激。
非常感谢,马科斯。
我不得不做类似的事情,这是我的代码,希望它可以帮助你。
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
CGRect aRect = self.bounds;
aRect.size.height -= kbSize.height;
CGRect activeRect = [activeTextField convertRect:activeTextField.frame toView:self];
if (!CGRectContainsPoint(aRect, activeRect.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeRect.origin.y-kbSize.height+10);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
self.activeTextField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
self.activeTextField = nil;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
此外,请确保在加载视图时设置通知观察者,如下所示:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
只需将 activeTextField 定义为 UITextField 并确保您想要移动的所有内容都包含在您的 scrollView 中(在您的情况下,您可能可以将 viewController 更改为 scrollView)。另外,确保你的 scrollView 的 contentSize 至少是 self.bounds.size。
希望这可以帮助。
I just had to do this in my application - Stakenborg's answer got me 80% there, but there were a couple of additional refinements I added to work with TableViews specifically.
The main bits are that:
The second part requires a little bit of indirection - the text fields belong to my custom cells, so I need to respond to the BeginEditing message there by sending a message to the UITableViewController.
Here's how it all comes together. In the UITableViewController:
@property (nonatomic, strong) NSIndexPath *editCellIndexPath;
@property (nonatomic) bool keyboardShowing;
//....
- (void)setEditRow:(UITableViewCell *)cell
{
self.editCellIndexPath = [self.tableView indexPathForCell:cell];
if (self.keyboardShowing)
{
[self.tableView scrollToRowAtIndexPath:self.editCellIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:true];
}
}
- (void)keyboardWillShow:(NSNotification *)sender
{
CGSize kbSize = [[[sender userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
NSTimeInterval duration = [[[sender userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIEdgeInsets edgeInsets = [self.tableView contentInset];
edgeInsets.bottom += kbSize.height;
UIEdgeInsets scrollInsets = [self.tableView scrollIndicatorInsets];
scrollInsets.bottom += kbSize.height;
self.keyboardShowing = true;
[UIView animateWithDuration:duration animations:^{
[self.tableView setContentInset:edgeInsets];
[self.tableView setScrollIndicatorInsets:scrollInsets];
}];
}
- (void)keyboardWillHide:(NSNotification *)sender
{
CGSize kbSize = [[[sender userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
NSTimeInterval duration = [[[sender userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIEdgeInsets edgeInsets = [self.tableView contentInset];
edgeInsets.bottom -= kbSize.height;
UIEdgeInsets scrollInsets = [self.tableView scrollIndicatorInsets];
scrollInsets.bottom -= kbSize.height;
self.keyboardShowing = false;
[UIView animateWithDuration:duration animations:^{
[self.tableView setContentInset:edgeInsets];
[self.tableView setScrollIndicatorInsets:scrollInsets];
}];
}
Then I have a weak property for the owningController in each of my custom UITableViewCells, and let the controller know when my cell is being text-edited. I use a TextView in one, and TextFields in another row, so I use these methods:
- (void)textViewDidBeginEditing:(UITextView *)textView
{
MyCustomTableController *itemControl = (MyCustomTableController *)self.owningController;
[itemControl setEditRow:self];
}
and
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
MyCustomTableController *itemControl = (MyCustomTableController *)self.owningController;
[itemControl setEditRow:self];
}
So far, this is working very well.