在ViewController中的两个视图上以编程方式设置自动布局约束
我正在尝试在ViewController上添加两个UIView.其中之一是普通的UIView,另一个是UITableView.我不太确定如何对它们设置高度限制,以使UITableView达到其内容所需的高度,而其他视图可以根据UITableView的高度调整自身大小.
I am trying to add two UIView on ViewController. One of them is plain UIView and other is UITableView. I am not quite sure how to set height constraint on them in such away that UITableView will be of height required for its content and other view can resize itself based on UITableView height.
下面是我用来设置这些约束的简单草图和代码.
Below is the simple sketch and code I am using for setting up those constraints.
NSDictionary *views = NSDictionaryOfVariableBindings(_infoView,_infoTableView);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[_infoView]"
options:0
metrics:nil
views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_infoView]-[_infoTableView]"
options:0
metrics:nil
views:views]];
// Width constraint
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.infoView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.infoTableView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.infoView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.infoTableView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0]];
任何评论/反馈将不胜感激. 谢谢.
Any comments / feedback would be highly appreciated. Thanks.
您处在正确的轨道上.这里的技巧是,您需要将tableView
的高度动态更新为它的contentSize.height
,这样infoView
会知道它应该垂直扩展还是缩小:
You are on the right track. The trick here is that you need to dynamically update the height of tableView
to its contentSize.height
so infoView
will know it should either be vertically expanded or shrunk:
首先,您将定义一个将保留高度限制的属性:
First you define a property that will hold the height constraint:
@property (nonatomic, strong) NSLayoutConstraint *tableViewHeightConstraint;
- (void)viewDidLoad {
...
self.tableViewHeightConstraint = [NSLayoutConstraint constraintWithItem:self.tableView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:350.0];
}
然后,您可以在tableView知道其contentSize (ex:viewDidAppear:animated
) 后立即更新高度约束:
Then you can update the height constraint right after tableView knows its contentSize (ex: viewDidAppear:animated
):
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.tableViewHeightConstraint.constant = self.tableView.contentSize.height;
[self.view layoutIfNeeded];
}
我为您创建了一个示例项目,因此您可以玩围绕它来更好地了解我打算做什么.
I created a sample project for you so you can play around with it to have a better understanding about what I intend to do.