Ios 在UITableView中使用自动布局强制UIView填充屏幕

Ios 在UITableView中使用自动布局强制UIView填充屏幕,ios,uitableview,uiview,autolayout,ios8,Ios,Uitableview,Uiview,Autolayout,Ios8,我正在尝试向我的表格添加一个背景视图,该视图的大小应该是伸展的,以便在旋转时始终填充表格的框架。为了简化这个问题,我只是尝试在表视图中添加一个红色的UIVIew,然后使用自动布局,以确保红色视图始终填充整个可见背景。如果我没有实现自动布局,在旋转时,你可以看到红色不再填充屏幕上的可见区域。但当我使用下面的代码实现自动布局时,屏幕上不再显示红色视图。我只看到默认的表视图backgroundColor。为什么,我的自动布局代码有什么问题 UIView *view = [[UIView alloc]

我正在尝试向我的表格添加一个背景视图,该视图的大小应该是伸展的,以便在旋转时始终填充表格的框架。为了简化这个问题,我只是尝试在表视图中添加一个红色的
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]];

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

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

除非您的单元格是清晰的,否则您将无法看到此视图,除非您将视图向下拉或滚动到底部。

我认为您的自动布局代码没有任何磨损,只是表格视图无法很好地与自动布局配合使用。您想用这段代码实现什么(为什么不只是更改表的背景色)?@rdelmar我试图向表中添加背景视图,但它不是纯色。准确地说,它将是一个
UIVisualEffectview
。红色视图演示了设置,而不需要设置模糊效果的复杂性。我只需要它总是填满桌子的高度/宽度。简单多了!非常感谢。