【问题标题】:Add a sticky view underneath the Navigation Bar在导航栏下方添加一个粘性视图
【发布时间】:2014-09-29 14:38:53
【问题描述】:

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

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

【问题讨论】:

  • 为什么不使用顶部布局指南并将视图的顶部设置为 topLayoutGuide.bottom?

标签: ios objective-c iphone ios7


【解决方案1】:

以下是有效的:

// 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】:

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

    如果它只应该在某些时候出现,您可以为其约束设置动画以显示/隐藏它。

    【讨论】:

    • 我通过 [self.view addSubview] 添加了它,但它没有坚持。我假设是因为这是一个 tableViewController,而不是一个带有 tableView 的 ViewController。
    • 啊,我错过了那个细节,我很抱歉。如果您要使用带有 UITableView 属性的 UIViewController,那么您可能会对视图层次结构等进行更多控制。使用 UITableViewController,我很确定 self.view 实际上是 tableView。所以你可能需要转换成一个普通的 UIViewController 来实现你想要的。
    猜你喜欢
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多