简体   繁体   English

向上移动UITextField而不向上移动整个视图

[英]Move UITextField up without moving whole view up

I have a UITableView inside a UIScrollView. 我在UIScrollView中有一个UITableView。 The tableView does not span the length of the view as I have a UITextView at the bottom of the view (to enter messages) so the table ends at the textview. tableView不会跨越视图的长度,因为我在视图的底部有一个UITextView(用于输入消息),因此表在textview处结束。 However, The scrollView does span the entire length of the view. 但是,scrollView确实会跨越视图的整个长度。 When the keyboard comes up, I move the entire view up with it,(so nothing is hidden) but that makes the top cells of the table inaccessible (as in, I don't need to see the cells, but when i try to scroll to them I can't get to them) as it is pushing the whole view up. 当键盘出现时,我将整个视图向上移动(因此没有任何隐藏物),但这使表的顶部单元格不可访问(例如,我不需要查看这些单元格,但是当我尝试查看时)滚动到他们,我无法到达他们),因为它推动了整个视图。 I am wondering if instead, I can push the scrollview underneath up, in turn having it push the keyboard up with it, and then have the tableview stay in place and scroll to the proper cell (for me in this case it is the most recent message, or last cell in the table). 我想知道是否可以将滚动视图向上推,依次向上推键盘,然后将桌面视图留在原处并滚动到适当的单元格(在这种情况下,这是最新的消息或表格中的最后一个单元格)。 Having the tableview stay in place will allow scrolling access to all the cells in the table. 将tableview保留在原位将允许滚动访问表中的所有单元格。 Am I going about this the right way or is there an easier and better way to accomplish this? 我是要以正确的方式进行操作,还是有一种更简便,更好的方法来完成此操作? I'm working on a messaging app, and I want the UI to behave in a similar way to the native messages app. 我正在开发一个消息传递应用程序,并且希望UI的行为类似于本机消息传递应用程序。 When the keyboard comes up, it brings the last message up above the top of the keyboard, but when the keyboard is visible you can still scroll to the top of the table. 当键盘出现时,它将最后一条消息带到键盘顶部上方,但是当键盘可见时,您仍然可以滚动到表格顶部。 I will post my code for the movement of the view. 我将发布用于视图移动的代码。

//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];




}

What I would do is NOT move the whole view up, but just shrink the scrollView's height by y amount. 我要做的是不向上移动整个视图,而只是将scrollView的高度缩小y个数量。

So instead of 所以代替

self.view.frame = CGRectOffset(self.view.frame, 0, y);

you could 你可以

CGRect scrlRect = self.myScrollView.frame;
self.myScrollView.frame = CGMakeRect(scrlRect.origin.x, scrlRect.origin.y, scrlRect.size.width, scrlRect.size.height+y);

The first step in dealing with this is modify the insets on the table. 处理此问题的第一步是修改表格上的插图。 This gist would help you ensure that the tableView (or scrollview) scrollable area is no longer covered by the keyboard: https://gist.github.com/TimMedcalf/9505416 要点将帮助您确保tableView(或scrollview)可滚动区域不再被键盘覆盖: https ://gist.github.com/TimMedcalf/9505416

It may reveal further issues, but that's always my first port of call. 它可能会揭示出更多的问题,但这始终是我的第一站电话。

In your keyboarWasShown method wright below code: 在您的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;
}

Now in your textFieldShouldBeginEditing delegate method of UITextField write below code: 现在,在UITextField textFieldShouldBeginEditing委托方法中,编写以下代码:

- (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];
    }
}

Above is from my current project and its working somewhat like what you want. 上面是我当前的项目,它的工作有点像您想要的。 Using this way may help you achive your task. 使用这种方式可以帮助您完成任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM