Echizzle
Echizzle

Reputation: 3459

Issues with programatic NSLayout constraints in tableViewCell

I keep getting weird constraint errors and the views in my cell don't show up, below are the cell class and error log.

My table view cell class

#import "TableViewCell.h"

@implementation TableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    
    self.translatesAutoresizingMaskIntoConstraints = NO;
    
    self.recipeImageView = [UIImageView new];
    self.recipeImageView.image = [UIImage imageNamed:@"imagePickerBackground"];
    self.recipeImageView.layer.cornerRadius = 20;
    self.recipeImageView.layer.borderColor = [[UIColor blackColor]CGColor];
    self.recipeImageView.layer.borderWidth = 1;
    self.recipeImageView.layer.masksToBounds = YES;
    [self addSubview:self.recipeImageView];
    
    self.descriptionLabel = [UILabel new];
    self.descriptionLabel.text = @"description";
    self.descriptionLabel.font = [UIFont fontWithName:@"Chalkduster" size:12];
    self.descriptionLabel.textColor = [UIColor blackColor];
    self.descriptionLabel.layer.cornerRadius = 20;
    self.descriptionLabel.layer.borderWidth = 1;
    self.descriptionLabel.layer.masksToBounds = YES;
    self.descriptionLabel.layer.borderColor = [[UIColor blackColor]CGColor];
    [self addSubview:self.descriptionLabel];
    
    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_descriptionLabel, _recipeImageView);
    
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[_recipeImageView]-10-|" options:0 metrics:nil views:viewsDictionary];
    
//        NSArray *constraintsTwo = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[_recipeImageView]-10-|" options:0 metrics:nil views:viewsDictionary];
    
    NSLayoutConstraint *equalConstraint = [NSLayoutConstraint constraintWithItem:self.descriptionLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.recipeImageView attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
    
    NSArray *verticalConstrains = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[_recipeImageView(==200)]-10-[_descriptionLabel(==70)]-10-|" options:0 metrics:nil views:viewsDictionary];
    
    [self addConstraints:constraints];
//        [self addConstraints:constraintsTwo];
    [self addConstraints:verticalConstrains];
    [self addConstraint:equalConstraint];
    
}

return self;
}

@end

here are the programatic registration methods in the view controller (datasource and delegate of the table view)

in view did load

- (void)viewDidLoad {
[super viewDidLoad];

self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self registerTableView:self.tableView];
[self.view addSubview:self.tableView];

[self.tableView reloadData]; 

[self setUpNavigationBar];

}

register method

- (void)registerTableView:(UITableView *)tableView {

[tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"cell"];

}

cell for row at index path

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

return cell; 

}

height for row (set at 300)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

return 300;
}

The error log (it doesn't crash so I can't put a breakpoint)

2015-10-26 15:22:05.524 Recipe Diary[33894:1375043] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x7f7f53eca790 h=--& v=--& UIImageView:0x7f7f53ecebd0.midX ==>", "<NSLayoutConstraint:0x7f7f53ecfb90 H:|-(10)-[UIImageView:0x7f7f53ecebd0] (Names: '|':TableViewCell:0x7f7f53ece0b0'cell' )>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f7f53ecfb90 H:|-(10)-[UIImageView:0x7f7f53ecebd0] (Names: '|':TableViewCell:0x7f7f53ece0b0'cell' )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2015-10-26 15:22:05.525 Recipe Diary[33894:1375043] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x7f7f53e077f0 h=--& v=--& UIImageView:0x7f7f53ecebd0.midY ==>", "<NSLayoutConstraint:0x7f7f53ecfcb0 V:|-(10)-[UIImageView:0x7f7f53ecebd0] (Names: '|':TableViewCell:0x7f7f53ece0b0'cell' )>", "<NSLayoutConstraint:0x7f7f53ecfd30 V:[UIImageView:0x7f7f53ecebd0(200)]>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f7f53ecfcb0 V:|-(10)-[UIImageView:0x7f7f53ecebd0] (Names: '|':TableViewCell:0x7f7f53ece0b0'cell' )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2015-10-26 15:22:05.664 Recipe Diary[33894:1375043] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x7f7f53ecdc80 h=--& v=--& V:[UIImageView:0x7f7f53ecebd0(0)]>", "<NSLayoutConstraint:0x7f7f53ecfd30 V:[UIImageView:0x7f7f53ecebd0(200)]>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f7f53ecfd30 V:[UIImageView:0x7f7f53ecebd0(200)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2015-10-26 15:22:05.744 Recipe Diary[33894:1375043] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x7f7f53e077f0 h=--& v=--& UIImageView:0x7f7f53ecebd0.midY ==>", "<NSAutoresizingMaskLayoutConstraint:0x7f7f53ecdc80 h=--& v=--& V:[UIImageView:0x7f7f53ecebd0(0)]>", "<NSAutoresizingMaskLayoutConstraint:0x7f7f53f32fb0 h=--& v=--& UILabel:0x7f7f53ecf310'description'.midY ==>", "<NSLayoutConstraint:0x7f7f53ecfd80 V:[UIImageView:0x7f7f53ecebd0]-(10)-[UILabel:0x7f7f53ecf310'description']>", "<NSLayoutConstraint:0x7f7f53ecfdd0 V:[UILabel:0x7f7f53ecf310'description'(70)]>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f7f53ecfd80 V:[UIImageView:0x7f7f53ecebd0]-(10)-[UILabel:0x7f7f53ecf310'description']>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2015-10-26 15:22:05.746 Recipe Diary[33894:1375043] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x7f7f53f392e0 h=--& v=--& V:[UILabel:0x7f7f53ecf310'description'(0)]>", "<NSLayoutConstraint:0x7f7f53ecfdd0 V:[UILabel:0x7f7f53ecf310'description'(70)]>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f7f53ecfdd0 V:[UILabel:0x7f7f53ecf310'description'(70)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2015-10-26 15:22:05.747 Recipe Diary[33894:1375043] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x7f7f53f36750 h=--& v=--& UIImageView:0x7f7f53c62320.midX ==>", "<NSLayoutConstraint:0x7f7f53e94600 H:|-(10)-[UIImageView:0x7f7f53c62320] (Names: '|':TableViewCell:0x7f7f53c612b0'cell' )>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f7f53e94600 H:|-(10)-[UIImageView:0x7f7f53c62320] (Names: '|':TableViewCell:0x7f7f53c612b0'cell' )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2015-10-26 15:22:05.759 Recipe Diary[33894:1375043] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x7f7f53f3a030 h=--& v=--& UIImageView:0x7f7f53c62320.midY ==>", "<NSLayoutConstraint:0x7f7f53e9c3a0 V:|-(10)-[UIImageView:0x7f7f53c62320] (Names: '|':TableViewCell:0x7f7f53c612b0'cell' )>", "<NSLayoutConstraint:0x7f7f53e9c420 V:[UIImageView:0x7f7f53c62320(200)]>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f7f53e9c3a0 V:|-(10)-[UIImageView:0x7f7f53c62320] (Names: '|':TableViewCell:0x7f7f53c612b0'cell' )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2015-10-26 15:22:05.760 Recipe Diary[33894:1375043] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x7f7f53f39c30 h=--& v=--& V:[UIImageView:0x7f7f53c62320(0)]>", "<NSLayoutConstraint:0x7f7f53e9c420 V:[UIImageView:0x7f7f53c62320(200)]>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f7f53e9c420 V:[UIImageView:0x7f7f53c62320(200)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2015-10-26 15:22:05.761 Recipe Diary[33894:1375043] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x7f7f53f3a030 h=--& v=--& UIImageView:0x7f7f53c62320.midY ==>", "<NSAutoresizingMaskLayoutConstraint:0x7f7f53f39c30 h=--& v=--& V:[UIImageView:0x7f7f53c62320(0)]>", "<NSAutoresizingMaskLayoutConstraint:0x7f7f53c4d870 h=--& v=--& UILabel:0x7f7f53ec94d0'description'.midY ==>", "<NSLayoutConstraint:0x7f7f53e9c470 V:[UIImageView:0x7f7f53c62320]-(10)-[UILabel:0x7f7f53ec94d0'description']>", "<NSLayoutConstraint:0x7f7f53e9c4c0 V:[UILabel:0x7f7f53ec94d0'description'(70)]>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f7f53e9c470 V:[UIImageView:0x7f7f53c62320]-(10)-[UILabel:0x7f7f53ec94d0'description']>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2015-10-26 15:22:05.767 Recipe Diary[33894:1375043] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x7f7f53c4bab0 h=--& v=--& V:[UILabel:0x7f7f53ec94d0'description'(0)]>", "<NSLayoutConstraint:0x7f7f53e9c4c0 V:[UILabel:0x7f7f53ec94d0'description'(70)]>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f7f53e9c4c0 V:[UILabel:0x7f7f53ec94d0'description'(70)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2015-10-26 15:22:05.769 Recipe Diary[33894:1375043] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x7f7f53c4d330 h=--& v=--& UIImageView:0x7f7f53ecb400.midX ==>", "<NSLayoutConstraint:0x7f7f53c5da10 H:|-(10)-[UIImageView:0x7f7f53ecb400] (Names: '|':TableViewCell:0x7f7f53ec96e0'cell' )>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f7f53c5da10 H:|-(10)-[UIImageView:0x7f7f53ecb400] (Names: '|':TableViewCell:0x7f7f53ec96e0'cell' )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2015-10-26 15:22:05.804 Recipe Diary[33894:1375043] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x7f7f53c4b6a0 h=--& v=--& UIImageView:0x7f7f53ecb400.midY ==>", "<NSLayoutConstraint:0x7f7f53c5de60 V:|-(10)-[UIImageView:0x7f7f53ecb400] (Names: '|':TableViewCell:0x7f7f53ec96e0'cell' )>", "<NSLayoutConstraint:0x7f7f53c5dee0 V:[UIImageView:0x7f7f53ecb400(200)]>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f7f53c5de60 V:|-(10)-[UIImageView:0x7f7f53ecb400] (Names: '|':TableViewCell:0x7f7f53ec96e0'cell' )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2015-10-26 15:22:05.806 Recipe Diary[33894:1375043] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x7f7f53c4d980 h=--& v=--& V:[UIImageView:0x7f7f53ecb400(0)]>", "<NSLayoutConstraint:0x7f7f53c5dee0 V:[UIImageView:0x7f7f53ecb400(200)]>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f7f53c5dee0 V:[UIImageView:0x7f7f53ecb400(200)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2015-10-26 15:22:05.839 Recipe Diary[33894:1375043] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x7f7f53c4b6a0 h=--& v=--& UIImageView:0x7f7f53ecb400.midY ==>", "<NSAutoresizingMaskLayoutConstraint:0x7f7f53c4d980 h=--& v=--& V:[UIImageView:0x7f7f53ecb400(0)]>", "<NSAutoresizingMaskLayoutConstraint:0x7f7f53c4d660 h=--& v=--& UILabel:0x7f7f53f454e0'description'.midY ==>", "<NSLayoutConstraint:0x7f7f53c5df30 V:[UIImageView:0x7f7f53ecb400]-(10)-[UILabel:0x7f7f53f454e0'description']>", "<NSLayoutConstraint:0x7f7f53c5df80 V:[UILabel:0x7f7f53f454e0'description'(70)]>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f7f53c5df30 V:[UIImageView:0x7f7f53ecb400]-(10)-[UILabel:0x7f7f53f454e0'description']>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2015-10-26 15:22:05.842 Recipe Diary[33894:1375043] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x7f7f53c4d6b0 h=--& v=--& V:[UILabel:0x7f7f53f454e0'description'(0)]>", "<NSLayoutConstraint:0x7f7f53c5df80 V:[UILabel:0x7f7f53f454e0'description'(70)]>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f7f53c5df80 V:[UILabel:0x7f7f53f454e0'description'(70)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. Message from debugger: Terminated due to signal 15

Upvotes: 1

Views: 109

Answers (1)

Echizzle
Echizzle

Reputation: 3459

I figured it out

instead of

self.translatesAutoresizingMaskIntoConstraints = NO;

it should be

self.recipeImageView.translatesAutoresizingMaskIntoConstraints = NO;
self.descriptionLabel.translatesAutoresizingMaskIntoConstraints = NO;

Upvotes: 0

Related Questions