我在一个iOS故事板中有一个场景,其中包含两个主要元素:一个MKMap和一个UITableView,见屏幕截图:

约束(Y轴上的约束)是:
我有三个案子:
对于案例1,我(以编程方式)更改高度约束的值,并将其设置为0。
在第二种情况下,我什么都不改变
第三个案子。我不知道该怎么办。
,我是否需要向地图添加另一个约束,如底部空间:具有更高优先级的0?
发布于 2015-02-17 07:20:01
有很多方法可以解决这一问题,但我个人要保持对高度约束的引用,这是
V:[mapView(==0)] (案例1),V:[mapView(==200)] (案例2),或V:[tableView(==0)] (例3)。因为最后一个约束位于不同的视图上,所以我的代码可能只需要删除旧的约束,创建新的约束,然后激活约束的布局。
因此,它看起来可能如下:
[self.heightConstraint.firstItem removeConstraint:self.heightConstraint];
NSLayoutConstraint *constraint;
if (constraintType == 1) {
constraint = [NSLayoutConstraint constraintWithItem:self.mapView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0.0];
} else if (constraintType == 2) {
constraint = [NSLayoutConstraint constraintWithItem:self.mapView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:200.0];
} else if (constraintType == 3) {
constraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0.0];
}
[constraint.firstItem addConstraint:constraint];
self.heightConstraint = constraint;
[UIView animateWithDuration:0.25 animations:^{
[self.view layoutIfNeeded];
}];https://stackoverflow.com/questions/28551698
复制相似问题