我一直在努力工作几个小时,并在stackoverflow和docs上进行了大量工作。但是我无法在我的应用程序中正确运行Apple代码。 这是完整的苹果代码,之后我会问我的问题:
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (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;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
[self.scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
1:为什么他们在这里调整插图?我还是不明白:
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
2:我正在使用此代码编写UITableView的子类,以便在我的应用程序中的所有表视图中使用它。我从来没有一个简单的表视图,通常它嵌入在superview中。或者说我通常在屏幕顶部有一个视图,一个位于底部,中间位于我的桌面视图之间。我想仅在文本字段隐藏在键盘下方并且只有这个文本字段可见时才移动我的表格视图。我是这么说的,因为至少在一个屏幕上显示的键盘一次只能显示约2个单元格。使用苹果代码,表视图得到了更多的滚动。到目前为止,我已经调整了代码(我的tableview的子类有两个属性,activeField和activeFieldOriginInSuperView(没有我认为这是不可能的?)但它仍然移动表视图太多了,相关的文本字段不是再见了......不知道出了什么问题?
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize keyBoardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect tableFrame = self.frame;
tableFrame.size.height -= keyBoardSize.height;
CGPoint activeFieldOrigin = self.activeFieldOriginInSuperView;
activeFieldOrigin.y -= self.contentOffset.y;
CGPoint activeFieldLowerEnd = CGPointMake(activeFieldOrigin.x, activeFieldOrigin.y + self.activeField.frame.size.height + 5);
if (!CGRectContainsPoint(tableFrame, activeFieldLowerEnd) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeFieldLowerEnd.y - tableFrame.size.height);
[self setContentOffset:scrollPoint animated:YES];
}
}
答案 0 :(得分:1)
一定会奏效。它适用于我最近的50个应用程序。 试试这个
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
- (void)keyboardDidShow: (NSNotification *) notif{
[UIView animateWithDuration:0.5f delay:0.0f options: UIViewAnimationOptionCurveEaseInOut animations:^{
_scrollView.contentOffset = CGPointMake(0, txtId_.frame.origin.y - 200);
} completion:^(BOOL finished){ }];
}
- (void)keyboardDidHide: (NSNotification *) notif{
[UIView animateWithDuration:0.5f delay:0.0f options: UIViewAnimationOptionCurveEaseInOut animations:^{
_scrollView.contentOffset = self.view.frame.origin;
} completion:^(BOOL finished){ }];
}