我一直在开发一个应用程序,我有一个UIViewController
,我已经添加了一个UITableView
。现在我想在tableview
上面的固定位置添加一个UIView
。
我尝试了以下代码,但UIView
与tableView
一起滚动:
- (void)viewDidLoad {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(250, 100, 50, 50)];
view.backgroundColor = [UIColor orangeColor];
[_tableView addSubview:view];
[_tableView bringSubviewToFront:view];
}
如何防止UIView
滚动?
嗯,因为UITableView
是UIScrollView
的子类,你可以尝试实现像"浮动视图"这样的东西。要做到这一点,你应该确保
- 浮动视图始终是层次结构中最顶层的视图
- 当
contentOffset
改变时,浮动视图的位置应该更新(这样当用户滚动时,它会在视觉上浮动在其他内容的顶部)
您可以尝试这样做(假设floatingHeaderViewCenter
是floatingView
的初始中心:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
self.floatingHeaderView.center = CGPointMake(floatingHeaderViewCenter.x + scrollView.contentOffset.x, floatingHeaderViewCenter.y + scrollView.contentOffset.y);
[scrollView bringSubviewToFront:floatingHeaderView];
}
虽然你最好有一个视图容器与两个子视图:你的表视图和浮动视图
您不应该将视图添加为表视图的子视图。相反,视图控制器视图应该是其他视图的容器,在这个例子中,是一个视图和一个表视图。将这两个添加为视图控制器视图的子视图。
如果视图(viewToBringFront)隐藏在后面,那么您可以通过添加以下行将其设置为前面:
self.viewToBringFront.layer.zPosition = MAXFLOAT;