I have implemented custom cell separator lines for my static table view cells by creating a view with a gray background, and I need to apply Autolayout constraints so that the lines will resize to fill the display width upon rotating to different orientations. I have done just that with the code below, provided from another SO question I asked. This is working great, except there's one problem. When I scroll down and then back up, these custom separator lines disappear. Selecting the cell causes them to appear again, but they will soon disappear again after scrolling.
I know this is caused by the Autolayout constraints, because previous Autolayout constraints I added did not cause this issue to occur. If you check out that linked question you'll learn I experienced several issues with these custom lines and was able to solve them all, except for this disappearing issue. If you could help me solve this issue I'd surely appreciate it.
This is exactly how I set up my separator lines - I do this in viewDidLoad
:
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[cell.contentView addSubview:imageView];
imageView.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(imageView);
//Add a line separator above a cell:
//these constraints ensure the separator line is visible above the cell's accessory view
[cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-(%f@750)-[imageView]-(-%f@750)-|", indent, self.tableView.rowHeight] options:0 metrics:0 views:viewsDictionary]];
[cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:[imageView(0.5)]-(%f@750)-|", self.tableView.rowHeight] options:0 metrics:0 views:viewsDictionary]];
My question is, why do these Autolayout constraints cause the views to disappear, and how can I tweak them to ensure they will always remain visible?