也许这是一个反复出现的问题,但我遇到了这个问题以及一些iOS概念。我有 ViewController ,每个部分都有静态表视图,三个部分和一些行。在行内,我有UITextFields。我要做的是阻止键盘隐藏我的底部屏幕UI文本字段。我刚刚尝试了Managing the Keyboard的苹果解决方案,但由于我没有得到附加到静态表视图的滚动视图背后的概念,我无法将这个想法实现到我的项目中。你们推荐任何地方去学习吗?对不起,如果无法解释我正在尝试做什么。我有点失落。
任何帮助将不胜感激。
非常感谢, 马科斯。
答案 0 :(得分:2)
我必须做类似的事情,这是我的代码,希望它可以帮到你。
- (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。
希望这有帮助。
答案 1 :(得分:0)
我只需要在我的应用程序中执行此操作 - Stakenborg的回答让我获得了80%,但是我还添加了一些额外的改进来专门用于TableViews。
主要内容是:
第二部分需要一点间接 - 文本字段属于我的自定义单元格,所以我需要通过向UITableViewController发送消息来响应BeginEditing消息。
这里是如何结合在一起的。在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];
}];
}
然后我在每个自定义UITableViewCells中都拥有了owningController的弱属性,让控制器知道我的单元格何时被文本编辑。我在一个中使用TextView,在另一行中使用TextFields,所以我使用这些方法:
- (void)textViewDidBeginEditing:(UITextView *)textView
{
MyCustomTableController *itemControl = (MyCustomTableController *)self.owningController;
[itemControl setEditRow:self];
}
和
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
MyCustomTableController *itemControl = (MyCustomTableController *)self.owningController;
[itemControl setEditRow:self];
}
到目前为止,这种方法运作良好。