Add a sticky view underneath the Navigation Bar

我只是一个虾纸丫 提交于 2020-01-03 02:43:09

问题


I've seen a few other examples, but I haven't seen anything that actually sticks. What I want is essentially what a tableViewHeader does. I have a tableViewController with a navigation bar. I want to put a view into place that shows some search criteria in a small bar directly below the navbar. But I need it to stick when the user swipes to view the results.

I've tried adding a UIView as a subview to the navigation bar, but it always sits at the top of the Navigation Bar, overlaying everything.


回答1:


Here's what worked:

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



回答2:


Why not add the view to your viewController's view, and set up its constraints so that it sits just below the navigationBar, but above the tableView?

If it's only supposed to be there some of the time, you can animate its constraints to show/hide it.



来源:https://stackoverflow.com/questions/26102474/add-a-sticky-view-underneath-the-navigation-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!