在导航栏下方添加粘滞视图

时间:2014-09-29 14:38:53

标签: ios objective-c iphone ios7

我已经看过其他几个例子,但我还没有看到任何其他的例子。我想要的基本上是tableViewHeader的功能。我有一个带导航栏的tableViewController。我想放置一个视图,在导航栏正下方的小栏中显示一些搜索条件。但是当用户滑动查看结果时,我需要坚持下去。

我尝试将UIView作为子视图添加到导航栏,但它始终位于导航栏的顶部,覆盖所有内容。

2 个答案:

答案 0 :(得分:2)

以下是有效的:

// 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];

答案 1 :(得分:1)

为什么不将视图添加到viewController的视图中,并设置其约束以使其位于navigationBar下方,但在tableView上方?

如果它只是某些时候应该存在,你可以设置它的约束来显示/隐藏它。

相关问题