Ios 添加约束时,表视图将消失
我有一个容器视图:Ios 添加约束时,表视图将消失,ios,objective-c,autolayout,nslayoutconstraint,Ios,Objective C,Autolayout,Nslayoutconstraint,我有一个容器视图: UITableView *tableView = self.searchResultsSourcesTVC.tableView; NSDictionary *views = NSDictionaryOfVariableBindings(tableView); tableView.translatesAutoresizingMaskIntoConstraints = NO; // without this line there are conflicts [self.sea
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
我有一个以编程方式添加到容器中的表视图:
self.searchResultsSourcesTVC = [storyboard instantiateViewControllerWithIdentifier:@"searchResultsSourcesTVC"];
[self.searchResultsContainer addSubview:self.searchResultsSourcesTVC.view];
这里的结果是表视图不会自动调整大小以适合容器;它似乎延伸到了屏幕的南部,以至于滚动条可以完全消失在屏幕的南部。但它确实显示了表格和搜索结果
因此,我尝试添加一个约束(我使用的是自动布局),以使表视图的垂直边界与容器视图的垂直边界相匹配:
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
现在根本没有桌子了。只是一片空白
我做错了什么?以编程方式将表视图添加到容器视图并使表视图的边界与容器视图的边界共同扩展的最佳方法是什么?谢谢您似乎没有足够的约束条件来充分描述您需要的尺码。例如,您似乎没有水平约束。通常需要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];
看起来你没有足够的约束来充分描述你需要的尺寸。例如,您似乎没有水平约束。通常需要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];
也为左/右侧创建约束,然后查看发生了什么…也为左/右侧创建约束,然后查看发生了什么。。。