【问题标题】:iPhone X TableView Footer problemsiPhone X TableView 页脚问题
【发布时间】:2019-04-08 04:34:44
【问题描述】:

好的,首先有两件事:我还是 iOS 开发的新手,我敢打赌这个问题已经回答过几次了,所以请随意将其链接到一个可行的解决方案????

现在问题来了:我有一个 UITableViewController,包括 自定义页脚视图。 (他们习惯在底部添加一个小边框,内置页脚只是纯灰色)。对于老式 iPhone,一切看起来都不错,但是在新的 X 上,我得到以下信息:

有没有办法将页脚视图全部扩展到视图区域的底部?

【问题讨论】:

  • 您只需将底部约束设置为底部布局指南的安全区域。

标签: ios iphone uitableview autolayout


【解决方案1】:

由于安全区域,默认布局是这样的。如果您想让页脚视图覆盖内容,我可以为您提供 2 个解决方案。

  • 改用UITableViewStyleGrouped,但页脚视图不会在屏幕上静止。

  • 我不认为这是最好的做法,但我认为布局应该是您正在寻找的。这有点棘手。步骤是:

    1. 创建高度等于底部安全区域的扩展视图。
    2. 将此扩展视图放在页脚视图下方,并将背景颜色设置为与页眉视图的背景颜色相同。
    3. 确保标题视图高度等于底部安全区域高度。 (这一步是难点)

示例代码

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UILabel *footerView = [[UILabel alloc] init];
    footerView.backgroundColor = [UIColor colorWithRed:0.968 green:0.968 blue:0.968 alpha:1]; //Section Header Background Color
    footerView.textAlignment = NSTextAlignmentRight;
    footerView.text = @"Footer";

    UIView *extendView = [[UIView alloc] init];
    extendView.translatesAutoresizingMaskIntoConstraints = NO;
    extendView.backgroundColor = footerView.backgroundColor;
    [footerView addSubview:extendView];

    [footerView addConstraints:@[
                                 [NSLayoutConstraint constraintWithItem:extendView
                                                              attribute:NSLayoutAttributeTop
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:footerView
                                                              attribute:NSLayoutAttributeBottom
                                                             multiplier:1.0
                                                               constant:0],
                                 [NSLayoutConstraint constraintWithItem:extendView
                                                              attribute:NSLayoutAttributeCenterX
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:footerView
                                                              attribute:NSLayoutAttributeCenterX
                                                             multiplier:1.0
                                                               constant:0],
                                 [NSLayoutConstraint constraintWithItem:extendView
                                                              attribute:NSLayoutAttributeWidth
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:footerView
                                                              attribute:NSLayoutAttributeWidth
                                                             multiplier:1.0
                                                               constant:0],
                                 [NSLayoutConstraint constraintWithItem:extendView
                                                              attribute:NSLayoutAttributeBottom
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:footerView
                                                              attribute:NSLayoutAttributeBottom
                                                             multiplier:1.0
                                                               constant:self.view.safeAreaInsets.bottom]
                                 ]];
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return self.view.safeAreaInsets.bottom;
}

屏幕录制 GIF

【讨论】:

  • 嘿SerKo,谢谢!完美运行。但是你知道为什么视图必须是 UILabel 吗?将其更改为 UITableViewHeaderFooterView 确实会再次导致相同的问题。我猜标准的 UITableViewHeaderFooterView 有某种高度限制,对吧?
  • 它可以是任何UIView,我认为你应该实现页脚/页眉视图的高度。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-29
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 2011-03-04
  • 1970-01-01
相关资源
最近更新 更多