首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态NSLayoutConstraint

动态NSLayoutConstraint
EN

Stack Overflow用户
提问于 2015-02-16 22:47:07
回答 1查看 196关注 0票数 1

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

约束(Y轴上的约束)是:

  • 地图顶部空间:0
  • 在地图和表格之间,空格:0
  • 表到下空间:0
  • 地图高度: 200

我有三个案子:

  1. 地图是隐藏的,所以我只看到了全屏的桌子。
  2. 这张地图高200 is,这张表适合屏幕。
  3. 地图是全屏的,表格是隐藏的。

对于案例1,我(以编程方式)更改高度约束的值,并将其设置为0。

在第二种情况下,我什么都不改变

第三个案子。我不知道该怎么办。

,我是否需要向地图添加另一个约束,如底部空间:具有更高优先级的0?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-17 07:20:01

有很多方法可以解决这一问题,但我个人要保持对高度约束的引用,这是

  • V:[mapView(==0)] (案例1),
  • V:[mapView(==200)] (案例2),或
  • V:[tableView(==0)] (例3)。

因为最后一个约束位于不同的视图上,所以我的代码可能只需要删除旧的约束,创建新的约束,然后激活约束的布局。

因此,它看起来可能如下:

代码语言:javascript
复制
[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];
}];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28551698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档