【问题标题】:Scrolling a UIView along with UITableView scroll滚动 UIView 和 UITableView 滚动
【发布时间】:2017-05-12 13:19:10
【问题描述】:

我有一个表视图,其中UITableViewCell 包含一个文本字段。当用户点击文本字段并出现键盘时,我在设置适当的内容插入后滚动到点击的行。

现在,我的表格视图顶部显示了一个 UIView,我需要在表格滚动时进行相应调整。

为此,我实现了scrollViewDidScroll: 方法,如下所示。虽然这在某些情况下有效,但在某些情况下它不起作用或给出随机结果。我在这里做错了吗?

- (void)scrollViewDidScroll:(UIScrollView *)iScrollView {
    if (self.hollowView) {
        CGPoint offset = iScrollView.contentOffset;
        CGRect hollowViewFrame = self.hollowView.hollowFrame;
        hollowViewFrame.origin.y += self.previousOffset - offset.y;
        self.previousOffset = offset.y;
        [self.hollowView resetToFrame:hollowViewFrame];
    }
} 

- (void)keyboardDidHide:(NSNotification *)iNotification {
    self.previousOffset = 0.0;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}

【问题讨论】:

  • 尝试使用IQKeyboardManager管理键盘。使用方便
  • 如果您使用autoLayout,则不应通过操作其框架来更改视图位置。
  • 如果你想与表格视图一起滚动视图,为什么不使用 tableViewheader。将您的视图放入 tableViewHeader 中,视图将随着表格滚动而沿 tableView 滚动

标签: ios objective-c iphone uitableview uiview


【解决方案1】:

你可以在 ios 中使用它来滚动表格。

-(void )keyboardWillShow:(NSNotification *)notification
 {

    NSDictionary* keyboardInfo = [notification userInfo];
    NSValue* keyboardFrameBegin =[keyboardInfovalueForKey:UIKeyboardFrameEndUserInfoKey];
    keyBoardHeight = keyboardFrameBegin.CGRectValue.size.height;
    vwSendBottomSpaceContraint.constant = keyBoardHeight
    [self performSelector:@selector(scrollToBottomAnimated) withObject:nil  afterDelay:0.1];

}

-(void)keyboardWillHide:(NSNotification*)notification
{

      vwSendBottomSpaceContraint.constant = 0;
      [self performSelector:@selector(scrollToBottomAnimated) withObject:nil afterDelay:0.1];

}

-(void)scrollToBottomAnimated
{

       NSInteger rows = [tableview numberOfRowsInSection:0];

       if(rows > 0)
       {
         [tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rows - 1 inSection:0]                            atScrollPosition:UITableViewScrollPositionBottomanimated:YES];       
       }

}

【讨论】:

  • 不,我不是要在键盘出现时滚动表格。这我已经在做。我要滚动的表格视图顶部有另一个空心视图。
【解决方案2】:

如果您想同时滚动这两个视图,那么您应该采用相同大小的两个视图,并且在以下方法中只需将滚动视图内容偏移设置为它们。它会很好地工作

enter code here - (void)scrollViewDidScroll:(UIScrollView *)iScrollView {
if (self.scrollView) // Check for the scrolled view 
{
    // Set the Tableview offset directly
  tableview.contentOffset = iScrollView.contentOffset  
}
else {
   //Set view scroll as per tableview scroll
       view.contentOffset = iScrollView.contentOffset   } }

//注意 :- 确保视图应该是滚动视图类型,否则视图会变得害怕或显示黑屏

谢谢..

【讨论】:

    【解决方案3】:

    使用这个库:

    https://github.com/hackiftekhar/IQKeyboardManager

    当点击文本字段时,它将处理视图的位置。

    【讨论】:

      【解决方案4】:

      所以,这篇文章对任何在同样情况下苦苦挣扎的人都很有用:

      - (void)scrollViewDidScroll:(UIScrollView *)iScrollView {
          if (self.hollowView) {
              CGRect frame = [self.tableView rectForRowAtIndexPath:self.expandedCellIndexPath];
              frame.origin.y -= self.tableView.contentOffset.y;
              [self.hollowView resetToFrame:frame];
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2010-10-20
        • 2021-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-05
        • 2017-12-05
        • 2017-12-01
        • 1970-01-01
        相关资源
        最近更新 更多