3

Problem

I can't seem to adopt Auto Layout into my existing project.

Details

I was having the same issue before as this question presentViewController: crash on iOS <6 (AutoLayout) but none of the provided answers were a solution for me: I'm using all storyboard views with no xibs. My 'Use Auto Layout' setting is already turned off and I am using nothing but iOS 6.

My view controller was initially crashing, so I set the constraints to be added with a delay and now my app crashes during any addConstraints: call. Worst part is that it won't tell me why.

Code

I will link my code, but its pretty straight forward.

-(void)addAllConstraints
{
    NSDictionary * views = NSDictionaryOfVariableBindings(_memoryImage, _peopleView, _contentHolder, _commentsTableView);
    NSArray * constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[_memoryImage]-50-[_peopleView]-0-[_contentHolder]-0-[_commentsTableView]" options:0 metrics:nil views:views];
    NSLog(@"Views %@, Constraints %@", views, constraints);
    [_peopleView addConstraints:constraints];
    [_memoryImage addConstraints:constraints];
    [_contentHolder addConstraints:constraints];
   [_commentsTableView addConstraints:constraints];
}

App crashes on _peopleView's call to addConstraints. Both the views and the NSLayoutConstraints are successfully created.

Any ideas? Thank you, Happy Holidays.

EDIT:

Adding Crash logs to show that nothing useful is showing:

2012-12-25 10:40:13.936 -----[4955:907] Views {
"_commentsTableView" = "<UITableView: 0x1eb6be00; frame = (0 372; 320 100); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x1e51ce00>; layer = <CALayer: 0x1e51cee0>; contentOffset: {0, 0}>";
"_contentHolder" = "<UIView: 0x1e5c6590; frame = (0 270; 320 112); layer = <CALayer: 0x1e5c27f0>>";
"_memoryImage" = "<UIButton: 0x1e5c4aa0; frame = (0 0; 320 280); opaque = NO; layer = <CALayer: 0x1e5c4b60>>";
"_peopleView" = "<UIView: 0x1f0ceea0; frame = (0 230; 320 50); layer = <CALayer: 0x1f0cf790>>";

Constraints (
"NSLayoutConstraint:0x1e51a880 V:[UIButton:0x1e5c4aa0]-(50)-[UIView:0x1f0ceea0]",
"NSLayoutConstraint:0x1e5ba4e0 V:[UIView:0x1f0ceea0]-(0)-[UIView:0x1e5c6590]",
"NSLayoutConstraint:0x1e51b860 V:[UIView:0x1e5c6590]-(0)-[UITableView:0x1eb6be00]"
)


}  
   (lldb) 
Community
  • 1
  • 1
Rob Caraway
  • 3,856
  • 3
  • 30
  • 37
  • It surely gives you some information in the crash log. For starters, your spacing constants should be in brackets. – jrturton Dec 25 '12 at 16:29
  • https://developer.apple.com/library/mac/#documentation/UserExperience/Conceptual/AutolayoutPG/Articles/formatLanguage.html#//apple_ref/doc/uid/TP40010853-CH3-SW11 Not true. And no, there is nothing in the crash log at all. – Rob Caraway Dec 25 '12 at 16:34
  • @RobCaraway There should be at least what kind of crash occurred. Is it `SIGABRT`? `EXC_BAD_ACCESS`? Or what? –  Dec 25 '12 at 16:52
  • I was wrong about the brackets, sorry. I think rdelmar has your answer, but for more information when debugging constraints you might find adding an all exceptions breakpoint to be useful. – jrturton Dec 26 '12 at 07:57
  • rdelmar was indeed correct. For clarification for future obverservers though, this was indeed a `SIGABRT` and Exception Break points were turned on when I was looking for solutions. – Rob Caraway Dec 26 '12 at 19:04

1 Answers1

6

Constraints are supposed to be added to the view that is the superview of the subviews. So, if these objects are in your main view, then you should have (and none of the other addConstraints: lines):

[self.view addConstraints:constraints];

Also, your dictionary, views, should be nil terminated (I don't know whether this is necessary or not. I've noticed in an Apple example that they didn't do this, but the function definition shows it with the nil).

rdelmar
  • 103,982
  • 12
  • 207
  • 218