UITableViewController的子控件不随着滑动

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

原文地址:https://www.cnblogs.com/fengtengfei/p/5671669.html