我在UIScrollView中有一个UITableView。 tableView没有跨越视图的长度,因为我在视图的底部有一个UITextView(用于输入消息),因此表格以textview结束。但是,scrollView确实跨越了视图的整个长度。当键盘出现时,我用它移动整个视图,(因此没有任何东西被隐藏),但这使得桌子的顶部单元格无法访问(因为,我不需要看到单元格,但是当我尝试滚动到它们我无法找到它们,因为它正在推动整个视图。我想知道是否相反,我可以将滚动视图推到下面,然后用它将键盘向上推,然后让tableview保持原位并滚动到正确的单元格(对我而言,在这种情况下它是最新的消息,或表中的最后一个单元格)。使tableview保持不变将允许滚动访问表中的所有单元格。我是以正确的方式解决这个问题还是有更简单,更好的方法来实现这一目标?我正在开发一个消息传递应用程序,我希望UI的行为方式与本机消息应用程序类似。当键盘出现时,它会将最后一条消息带到键盘顶部上方,但当键盘可见时,您仍然可以滚动到表格的顶部。我将发布我的代码以移动视图。
//call move view method and scroll to last cell in table
-(void) keyboardWasShown:(NSNotification*)aNotification
{
NSLog(@"Keyboard was shown");
[self moveView:[aNotification userInfo] up:YES];
[self.chatTable scrollRectToVisible:CGRectMake(0, self.chatTable.contentSize.height - self.chatTable.bounds.size.height, self.chatTable.bounds.size.width, self.chatTable.bounds.size.height) animated:YES];
}
-(void) keyboardWillHide:(NSNotification*)aNotification
{
NSLog(@"Keyboard will hide");
[self moveView:[aNotification userInfo] up:NO];
}
- (void)moveView:(NSDictionary*)userInfo up:(BOOL)up
{
CGRect keyboardEndFrame;
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]getValue:&keyboardEndFrame];
UIViewAnimationCurve animationCurve;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey]
getValue:&animationCurve];
NSTimeInterval animationDuration;
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]
getValue:&animationDuration];
// Get the correct keyboard size to we slide the right amount.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
int y = keyboardFrame.size.height * (up ? -1 : 1);
self.view.frame = CGRectOffset(self.view.frame, 0, y);
[UIView commitAnimations];
}
答案 0 :(得分:0)
我要做的不是移动整个视图,而是将scrollView的高度缩小y量。
所以而不是
self.view.frame = CGRectOffset(self.view.frame, 0, y);
你可以
CGRect scrlRect = self.myScrollView.frame;
self.myScrollView.frame = CGMakeRect(scrlRect.origin.x, scrlRect.origin.y, scrlRect.size.width, scrlRect.size.height+y);
答案 1 :(得分:0)
处理此问题的第一步是修改表上的insets。这个要点可以帮助您确保键盘不再覆盖tableView(或scrollview)可滚动区域:https://gist.github.com/TimMedcalf/9505416
它可能会揭示更多问题,但这始终是我的第一个停靠点。
答案 2 :(得分:0)
在keyboarWasShown
方法下面的代码:
- (void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
float kbHeight = kbSize.width > kbSize.height ? kbSize.height : kbSize.width;
self.keyboardHeight.constant = kbHeight+5;
}
现在,textFieldShouldBeginEditing
代理方法UITextField
写下以下代码:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
CGPoint point = [textField.superview convertPoint:textField.frame.origin toView:yourTableViewHere];
NSIndexPath *indexPath = [tblAsset indexPathForRowAtPoint:point];
CGRect tableViewVisibleRect = [tblAsset rectForRowAtIndexPath:indexPath];
if (point.y > (tableViewVisibleRect.size.height - self.keyboardHeight.constant))
{
CGPoint contentOffsetPoint = tblAsset.contentOffset;
contentOffsetPoint.y -= self.keyboardHeight.constant;
CGSize contectSize = tblAsset.contentSize;
contectSize.height += self.keyboardHeight.constant;
[tblAsset setContentSize:contectSize];
[tblAsset setContentOffset:contentOffsetPoint animated:NO];
}
}
上面是我目前的项目,它的工作方式与你想要的一样。使用这种方式可以帮助您完成任务。