AutoLayout 视图使应用程序在 popViewController 上崩溃

AutoLayout views make app crash on popViewController(AutoLayout 视图使应用程序在 popViewController 上崩溃)

本文介绍了AutoLayout 视图使应用程序在 popViewController 上崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最终更新 - 已修复

我已经解决了这个问题.似乎在玩耍时(这是我在 Swift 中使用 AutoLayout 的第一个项目),我已经更改了 contentCompressionResistancePrioritycontentHuggingPriority 的一些视图.取出该代码并将我的所有视图重置为 IB 中的默认值解决了我的问题.

I have fixed this. It seems that while playing around (this is my first project in Swift and using AutoLayout) I have changed the contentCompressionResistancePriority and contentHuggingPriority for some of my views. Taking that code out and reseting all my views to default values in IB fixed my problem.

首发

所以我在 iOS 上使用 AutoLayout 来定位我的动态视图.这一切都很好,很容易直到我弹出我的一个视图控制器.该应用程序崩溃,并显示一条非常无用的错误消息,如下所示:

So I am using AutoLayout on iOS to position my dynamic views. It's all working nice and easy until I pop one of my view controllers. The app crashes with a really not helpful error message that goes something like this:

...
internal error.  Cannot find an outgoing row head for incoming head UIImageView:0xd049d50.Width{id: 730}, which should never happen.'
...

我一直在四处寻找并搜索网络,但我无法修复它.虽然我发现我的代码中有一行可以产生影响.在我所说的视图控制器中,我有一堆使用 AutoLayout 的 UIImageView 以及我从网络加载的图像.

I've been pocking around and searched the web but I cannot fix it. I've discovered though that there is one line in my code that can make a difference. In my said view controller I have a bunch of UIImageViews that are using AutoLayout and who's images I load from the web.

如果我没有将接收到的图像设置为它们,而是在 Swift<中设置一个空图像([[UIImage alloc] init]UIImage())/strong> 就像我的情况一样)它不再崩溃了.

If instead of setting my received image to them I set an empty one ([[UIImage alloc] init] or UIImage()) in Swift as is my case exactly) it doesn't crash anymore.

我什至尝试从应用程序包中设置一个虚拟图像,但这也会导致它崩溃.

I even tried setting a dummy image from the app bundle but that makes it crash too.

任何建议将不胜感激!

更新 1

再次查看代码,我还发现有一个与那些 UIImageViews 相关的约束,这也会使崩溃在消除时消失.这是一个纵横比约束,看起来像这样

Looking trough the code once more I also found out that there is a constraint related to those UIImageViews that also makes the crash to go away when eliminated. It's an aspect ratio constraint and it looks like this

imageView.addConstraint(NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: imageView, attribute: NSLayoutAttribute.Height, multiplier: 8.0 / 5.0, constant: 0.0))

我做错了吗?我真的需要满足这个纵横比,所以我不能真正删除它

Am I doing something wrong? I really need this aspect ratio to be satisfied so I can't really remove it

更新 2

再搞砸一点,我想出了一个让它工作的方法.但我对它不满意,因为它没有让我明白发生了什么,而且它有点像 hack

Messing a little bit more with it I've figured a way to make it work. But I'm not happy with it 'cause it didn't made me understand what's going on and it's kind of a hack

让我给你布置我的视图结构:

Let me layout my view structure to you:

  • 查看
    • 滚动视图
      • 内容视图
        • 封面 ImageView(来自 IB)
        • 标题标签(来自 IB)
        • ...更多标签或 ImageViews 在代码中的随机顺序...

        这些视图的放置方式是这样的:

        The way these views are placed is like this:

        • Cover ImageViewContentView 一样宽,纵横比为 8:5,顶部没有空间
        • 每个标签都有一个10px的前导空格和一个10px的尾随空格
        • 每个 ImageViewContentView 一样宽,纵横比为 8:5
        • 所有视图之间都有 10px 的间隙,最后一个与 ContentView
        • 的底部间距为 10px
        • The Cover ImageView is as wide as the ContentView and has an aspect ratio of 8:5 with no space on top
        • Each Label has a 10px leading space and a 10px trailing space
        • Each ImageView is as wide as the ContentView and has an aspect ratio of 8:5
        • All views have a 10px gap beewtwen them and the last one has 10px bottom spacing to the ContentView

        我对崩溃的解决方法是从 viewWillDisappear 上的堆栈中删除 ContentView,但这会使 ViewController 弹出时屏幕闪烁.

        My fix to my crash was to remove ContentView from the stack on viewWillDisappear but that makes the screen flash when the ViewController is popped.

        我已经检查了我的约束创建代码 1000 次,这似乎是正确的.如果你想看,请告诉我,我会在这里发布

        I've checked my constraints creating code a 1000 times and it seems right. If you want to see it let me know and I'll post it over here

        推荐答案

        把这个放在这里,以防有人遇到与动态代码生成的 aspectRatio 约束相同的问题.我在纵横比约束中切换了 HeightWidth 关系的顺序(与问题中的比较):

        Just putting this here in case anyone has the same issue with a dynamic, code-generated aspectRatio constraint. I switched the order of the Height and Width relationship in the aspect-ratio constraint (compare to the one in the question):

        aspectConstraint = NSLayoutConstraint(item: cardMedia, attribute: NSLayoutAttribute.Height , relatedBy: NSLayoutRelation.Equal, toItem: cardMedia, attribute: NSLayoutAttribute.Width, multiplier: aspect, constant: 0.0)
        

        其中乘数aspect的计算公式为:

        Where the multiplier aspect is calculated by:

        let aspect = image.size.height / image.size.width
        

        这似乎阻止了崩溃的发生.希望这对某人有所帮助.

        And that seems to stop the crash from happening. Hope this helps someone.

        这篇关于AutoLayout 视图使应用程序在 popViewController 上崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:AutoLayout 视图使应用程序在 popViewController 上崩溃

基础教程推荐