UITableViewController的子控件不随着滑动


我们知道有时候使用UITableViewController简单便捷,省事,但是如果我们使用了addSubview,无论是[self.view addSubview:view]还是[self.tableView addSubview:view],均会发现添加的view是会随着tabelview的滑动而滑动的。这是没有办法避免的,UITableViewController的view就是tableview。所以效果是一样的。

所以一般不建议直接使用UITableViewController而是使用UIViewController,手动添加UITableview。但是如果已经使用了,怎样固定子控件,不让其随着UITableview而滚动。

在上面固定子控件

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGRect frame = self.floatingView.frame;
    frame.origin.y = scrollView.contentOffset.y;
    self.floatingView.frame = frame;

    [self.view bringSubviewToFront:self.floatingView];
} 

在下面固定子控件

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGRect frame = self.floatingView.frame;
    frame.origin.y = scrollView.contentOffset.y + self.tableView.frame.size.height - self.floatingView.frame.size.height;
    self.floatingView.frame = frame;

    [self.view bringSubviewToFront:self.floatingView];
}	

参照
How to put buttons over UITableView which won't scroll with table in iOS

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-03-05
  • 2021-08-25
  • 2022-12-23
  • 2022-01-08
  • 2021-07-18
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-01
  • 2022-12-23
  • 2021-05-22
  • 2022-12-23
  • 2021-11-08
相关资源
相似解决方案