Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

ios - AutoLayout views make app crash on popViewController

Final Update - Fixed

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.

Initial Post

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.'
...

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.

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.

Any suggestions would be much appreciated!

Update 1

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

Update 2

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:

  • View
    • Scroll View
      • ContentView
        • Cover ImageView (from IB)
        • Title Label (from IB)
        • ... more Labels or ImageViews in random order from code ...

The way these views are placed is like this:

  • 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

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...