【问题标题】:Using auto layout to enforce UIView in UITableView fill the screen使用自动布局强制 UITableView 中的 UIView 填满屏幕
【发布时间】:2014-09-16 23:02:34
【问题描述】:

我正在尝试向我的表格添加一个背景视图,该视图应该在大小上拉伸以始终在旋转时填充表格的框架。为了简化问题,我只是尝试在表格视图中添加一个红色UIVIew,然后使用自动布局以确保红色视图始终填充整个可见背景。如果我没有实现自动布局,旋转后你可以看到红色不再填充屏幕上的可见区域。但是当我使用下面的代码实现自动布局时,屏幕上不再显示红色视图。我只看到了默认的表格视图backgroundColor。这是为什么,我的自动布局代码有什么问题?

UIView *view = [[UIView alloc] initWithFrame:self.tableView.frame];
view.backgroundColor = [UIColor redColor];
[self.tableView addSubview:view];
[self.tableView sendSubviewToBack:view];

[view setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.tableView addConstraint:[NSLayoutConstraint constraintWithItem:view
                                                                           attribute:NSLayoutAttributeTop
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:self.tableView
                                                                           attribute:NSLayoutAttributeTop
                                                                          multiplier:1
                                                                            constant:0]];
                [self.tableView addConstraint:[NSLayoutConstraint constraintWithItem:view
                                                                           attribute:NSLayoutAttributeBottom
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:self.tableView
                                                                           attribute:NSLayoutAttributeBottom
                                                                          multiplier:1
                                                                            constant:0]];
                [self.tableView addConstraint:[NSLayoutConstraint constraintWithItem:view
                                                                           attribute:NSLayoutAttributeLeading
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:self.tableView
                                                                           attribute:NSLayoutAttributeLeading
                                                                          multiplier:1
                                                                            constant:0]];
                [self.tableView addConstraint:[NSLayoutConstraint constraintWithItem:view
                                                                           attribute:NSLayoutAttributeTrailing
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:self.tableView
                                                                           attribute:NSLayoutAttributeTrailing
                                                                          multiplier:1
                                                                            constant:0]];

【问题讨论】:

  • 我认为您的自动布局代码没有任何问题,只是表格视图不能很好地与自动布局配合使用。你想用这段代码完成什么(为什么不只是改变表格的背景颜色)?
  • @rdelmar 我正在尝试向表格添加背景视图,但它不是纯色。准确地说,它将是UIVisualEffectview。红色视图演示了设置,没有设置模糊效果的复杂性。我只需要它来填充表格的高度/宽度。

标签: ios uitableview uiview autolayout ios8


【解决方案1】:

您应该对表格视图使用 backgroundView 属性。只需使用 alloc init 创建您的视图(无需框架或约束)并将该视图设置为表格视图的背景视图,

    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor redColor];
    self.tableView.backgroundView = view;

除非您的单元格清晰,否则在您将视图拉下或滚动到底部之前,您不会看到此视图。

【讨论】:

  • 简单多了!谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多