mkc*_*842 1 objective-c ios autolayout nslayoutconstraint
我有一个容器视图:
我有一个表格视图,我以编程方式添加到容器中:
self.searchResultsSourcesTVC = [storyboard instantiateViewControllerWithIdentifier:@"searchResultsSourcesTVC"];
[self.searchResultsContainer addSubview:self.searchResultsSourcesTVC.view];
Run Code Online (Sandbox Code Playgroud)
这样的结果是表视图不会自动调整大小以适应容器; 它似乎在屏幕的南边延伸了很多,只要滚动条可以完全消失在屏幕的南边.但它确实显示了表格和搜索结果.
所以我试图添加一个约束(我使用自动布局)使表视图的垂直边界与容器视图的垂直边界匹配:
UITableView *tableView = self.searchResultsSourcesTVC.tableView;
NSDictionary *views = NSDictionaryOfVariableBindings(tableView);
tableView.translatesAutoresizingMaskIntoConstraints = NO; // without this line there are conflicts
[self.searchResultsContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tableView]|" options:0 metrics:Nil views:views]];
[self.view layoutIfNeeded]; // not sure whether this line is necessary
Run Code Online (Sandbox Code Playgroud)
现在根本没有桌子.只是一个空白的视图.
我究竟做错了什么?以编程方式将表视图添加到容器视图并使表视图的边界与容器视图的边界共同延伸的最佳方法是什么?谢谢
看起来你没有足够的限制来完全描述你需要的尺寸.例如,您似乎没有水平约束.您通常需要4个约束来完全表达要布局的视图的大小; 特别是你需要一个中心X,中心Y,宽度和高度.
例如:
NSLayoutConstraint *con1 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterX relatedBy:0 toItem:view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
NSLayoutConstraint *con2 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
NSLayoutConstraint *con3 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:0 toItem:view attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
NSLayoutConstraint *con4 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:0 toItem:view attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
NSArray *constraints = @[con1, con2, con3, con4];
[self.searchResultsController addConstraints:constraints];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2114 次 |
| 最近记录: |