我见过其他一些例子,但我没有看到任何真正粘附的东西。我想要的基本上是tableViewHeader的功能。我有一个带有导航栏的tableViewController。我想将视图放置在位于导航栏正下方的小栏中显示一些搜索条件的位置。但是当用户滑动查看结果时,我需要将其粘住。在导航栏下添加一个粘滞视图
我已经尝试添加一个UIView作为导航栏的子视图,但它总是坐在导航栏的顶部,覆盖了一切。
我见过其他一些例子,但我没有看到任何真正粘附的东西。我想要的基本上是tableViewHeader的功能。我有一个带有导航栏的tableViewController。我想将视图放置在位于导航栏正下方的小栏中显示一些搜索条件的位置。但是当用户滑动查看结果时,我需要将其粘住。在导航栏下添加一个粘滞视图
我已经尝试添加一个UIView作为导航栏的子视图,但它总是坐在导航栏的顶部,覆盖了一切。
这里是什么工作:
// Create a custom navigation view
_navigationView = [[UIView alloc] init];
_navigationView.backgroundColor = [UIColor whiteColor];
_navigationView.frame = CGRectMake(0.0f,
self.navigationController.navigationBar.frame.origin.y + 44.0f,
self.view.frame.size.width,
30.0f);
// Create bottom border for the custom navigation view
_navigationViewBorder = [[UIView alloc] init];
_navigationViewBorder.backgroundColor = [UIColor darkGrayColor];
_navigationViewBorder.tag = 1;
_navigationViewBorder.frame = CGRectMake(0.0f,
self.navigationController.navigationBar.frame.origin.y + 74.0f,
self.view.frame.size.width,
0.5f);
// Add labels for date, results, and the time range
_dateDisplay = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 70, 15)];
[_dateDisplay setFont:[UIFont fontWithName:@"HelveticaNeue" size:13]];
[_dateDisplay setText:@""];
_timeRangeDisplay = [[UILabel alloc] initWithFrame:CGRectMake(98, 0, 135, 15)];
[_timeRangeDisplay setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:13]];
[_timeRangeDisplay setText:@""];
_resultsDisplay = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 80, 15)];
[_resultsDisplay setFont:[UIFont fontWithName:@"HelveticaNeue" size:13]];
[_resultsDisplay setText:@""];
[_navigationView addSubview: _dateDisplay];
[_navigationView addSubview: _timeRangeDisplay];
[_navigationView addSubview: _resultsDisplay];
// Add two views to the navigation bar
[self.navigationController.navigationBar.superview insertSubview:_navigationView belowSubview:self.navigationController.navigationBar];
[self.navigationController.navigationBar.superview insertSubview:_navigationViewBorder belowSubview:_navigationView];
为什么不将视图添加到您的viewController的视图,并设置其约束,以便它位于navigationBar下方,但在tableView上方?
如果它只是在某些时候存在,您可以动画化它的约束来显示/隐藏它。
我通过[self.view addSubview]添加了它,但没有粘住。我假设因为这是一个tableViewController,而不是一个ViewController和一个tableView。 – jlrolin 2014-09-29 15:01:11
啊,我错过了那个细节,我的歉意。如果你使用带有UITableView属性的UIViewController,那么你可能对视图层次结构有更多的控制权。使用UITableViewController,我很确定self.view实际上是tableView。所以你可能需要转换成一个普通的UIViewController来实现你正在寻找的东西。 – 2014-09-29 15:11:19
您可以使用'tableView'上的'setContentInset'来将其向下移动,然后通过向下移动表视图的方式在所需的可用空间中插入一个视图和所需的控件。 – CapturedTree 2017-07-18 18:07:13
为什么不使用顶部的布局指南和视图的顶部设置为topLayoutGuide.bottom? – CapturedTree 2017-09-08 22:08:38