嵌入式.xib出现在错误的位置

时间:2014-09-10 17:28:38

标签: objective-c xcode interface-builder autolayout ios8

我希望这是一个iOS 8错误(它仍然可能;在iOS 7中一切正常)但我仍然在iOS 8 GM Seed中看到它。

我在DetailView.xib中嵌入了CategoriesView.xib。显示DetailView.xib时,占位符UIView显示在正确的位置,但CategoriesView.xib的内容恰好比它们应该的位置高出72px。我正在使用Autolayout。 72px似乎是20px状态栏+ 44px导航栏+ 8px自动布局约束从图像的顶部。

enter image description here

在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宽。因此,最后一个类别图标会被拉伸:

enter image description here

我在这里不知所措。我知道这是非常微妙的,但任何想法?

1 个答案:

答案 0 :(得分:0)

我尝试了所有我能想到的东西,但却无法让它发挥作用。

我的解决方案?我只是创建了一个全新的文件并重建了所有元素和所有约束。而且效果很好。

如此令人沮丧,但事情终于恢复正常了。我认为它与从Xcode 5到Xcode 6的过渡有关。