【发布时间】: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