【发布时间】:2014-09-10 17:28:37
【问题描述】:
我希望这是一个 iOS 8 错误(它仍然可能是;在 iOS 7 中一切正常),但我仍然在 iOS 8 GM Seed 中看到它。
我在 DetailView.xib 中嵌入了 CategoriesView.xib。当 DetailView.xib 显示时,占位符 UIView 显示在正确的位置,但 CategoriesView.xib 的内容出现在它们应有的位置上方 72px 处。我正在使用自动布局。 72px 似乎是 20px 状态栏 + 44px 导航栏 + 8px 从图像顶部开始的自动布局约束。
在 CategoriesView.xib 中,实际上有 5 个小图标代表每个可能的类别。每个图标都有一个宽度/高度约束和一个前导空间约束,以从其邻居添加填充。通常一个屏幕只会显示 1-2 个类别,我使用以下代码完成此操作:
// all width constraints are part of the categoryViewsWidthConstraints Outlet Collection
// all padding constraints are part of the categoryViewsSpacingConstraints Outlet Collection
// loop through my outlet collection, and set all width and padding constraints to 0
for (int i = 0; i < self.categoryViewsWidthConstraints.count; i++) {
[self.categoryViewsWidthConstraints[i] setConstant:0.0];
[self.categoryViewsSpacingConstraints[i] setConstant:0.0];
}
// now, for the categories that need to show, add the width and padding constraints back
for (int i = 0; i < self.categoryIds.count; i++) {
NSNumber *categoryId = self.categoryIds [i];
[self.categoryViewsWidthConstraints[categoryId.integerValue - 1] setConstant:20.0];
[self.categoryViewsSpacingConstraints[categoryId.integerValue - 1] setConstant:8.0];
}
同样,这在 iOS 7 中运行良好。
当我在 CategoriesView.m 中设置时,我认为我已经完成了这项工作:
self.contentView.translatesAutoResizingMaskintoConstraints = true.
这为我提供了我所追求的正确垂直位置,但不幸的是,我遇到了自动布局错误,因为它认为 CategoriesView 需要正好是 132px 宽。因此,最后一个类别图标被拉伸:
我在这里不知所措。我知道这很微妙,但有什么想法吗?
【问题讨论】:
标签: objective-c xcode interface-builder autolayout ios8