I have to do this UIview subclass with out using any xib or storyboard, it needs to be 1 file so I need to get this tableview setup in code and avoid subclassing UITableViewCell.
I am trying to get an image view to be set as V:|-5-[imageview]-5-| and H:[iamgeview(width = height)]-5-| inside the cell's contentView.
all I get is that all my added constraint get broken and the cell is not where I want it to be.
the base cell is in basic style cell. All I am trying to do is add a image view at the end of the view and this is turning in a nightmare at the moment.
I am setting the image view as follows in the code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
POI *poi = [self.results objectAtIndex:indexPath.row];
NSString *text = poi.name;
cell.textLabel.text = text;
UIImageView *image = [[UIImageView alloc] init];
image.image = [UIImage imageWithColor:[UIColor blueColor]];
[cell.contentView addSubview:image];
NSDictionary* viewDic = @{@"image":image};
cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[image]"
options:0
metrics:nil
views:viewDic]];
[cell.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:[image(w)]-5-|"
options:0
metrics:@{@"w":cell.contentView.frame.size.heigh}
views:viewDic]];
return cell;
}
when the cell loads I get this error :
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fb7b636aaa0 H:[img(44)] (Names: img:0x7fb7b6368fc0 )>
and <NSLayoutConstraint:0x7fb7b6369380 V:[img(44)] (Names: img:0x7fb7b6368fc0 )>
and <NSLayoutConstraint:0x7fb7b636aaf0 H:[img]-(5)-| (Names: Contentview:0x7fb7b6372a30, img:0x7fb7b6368fc0, '|':Contentview:0x7fb7b6372a30 )>
and <NSLayoutConstraint:0x7fb7b63697e0 V:|-(5)-[img] (Names: img:0x7fb7b6368fc0, Contentview:0x7fb7b6372a30, '|':Contentview:0x7fb7b6372a30 )>
You must set translatesAutoresizingMaskIntoConstraints=NO
for any view on which you are using autolayout constraints. If you don't do this, autolayout constraints matching the autoResizingMask
will be automatically added to the view in addition to any constraints you add manually. This will almost always result in an over-constrained view in which one or more constraints must be broken in order to layout the view.