I have a UIScrollView that is to show UIImageViews. The ImageViews are programmatically inserted at runtime based on how many images are saved by the user previously. I get the error below:
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)
"<NSLayoutConstraint:0x17029cd90 H:[UIView:0x15cd31e70]-(0)-| (Names: '|':UIScrollView:0x15cd326b0 )>", "<NSLayoutConstraint:0x17029d060 UIScrollView:0x15cd326b0.trailingMargin == UIView:0x15cd31e70.trailing>"
I do the basic Autolayout thing where the scrollview is pinned to all four sides at 0 points. I then add a contentView as a subview (plain UIView) of the UIScrollView which is also pinned to all four sides at 0 points.
EDIT Storyboard constraints image
I give the contentView a width in code like so:
CGSize pagesScrollViewSize = self.scrollView.frame.size;
NSDictionary *views = @{ @"contentView" : self.contentView};
NSDictionary *metrics = @{ @"width" : @(pagesScrollViewSize.width * self.mutableArray.count) };
NSArray *constraints;
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[contentView(width)]-|" options:0 metrics:metrics views:views];
[self.scrollView addConstraints:constraints];
[self loadVisiblePages];
The UIImageViews are added like so where UIImageViews are added based on the number of pages set when the segue to the ViewController occurs.
-(void)loadVisiblePages{
CGRect frame = self.scrollView.bounds;
frame.origin.x = frame.size.width * self.page;
frame.origin.y = 0.0f;
ImageForArchiving *newImageObject = (ImageForArchiving*)self.mutableArray[page];
UIImage *imageForNewPageView = newImageObject.image;
UIImageView *newPageView = [[UIImageView alloc] initWithFrame:frame];
[newPageView setTranslatesAutoresizingMaskIntoConstraints:YES];
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.image = imageForNewPageView;
[self.scrollView addSubview:newPageView];
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
}
}
Additionally, when I scroll the UIScrollView the images displayed change size erratically on rotation. I think that this is just a consequence of the above warning and the fact that I haven't layed out the UIImageViews yet. What does the above warning mean in this context and how do I fix it?
It seems you have pinned trailingMargin of scrollView to contentView.trailing. Change scrollView.Trailing Margin to scrollView.Trailing for this constraint You can do this in the activity inspector in the storyboard after selecting your constraint.
Alternatively, clear all constraints on your contentView. Then while adding pinning constraints again uncheck Constrain to margins
and set all constants 0.
AND
Change this line in your code
NSArray *constraints;
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[contentView(width)]-|" options:0 metrics:metrics views:views];
[self.scrollView addConstraints:constraints];
with this:
NSArray *constraints;
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView(width)]|" options:0 metrics:metrics views:views];
[self.scrollView addConstraints:constraints];
Using @"H:|-[contentView(width)]-|"
in visual format means pinning your contentView to the superView's margins and adds a space of 8 pts between the superView and subView. In your storyboard constraints you had set up constraints with the Trailing and Leading edges of the UIScrollView, while in the programmatically added constraint you had used Trailing Margin and Leading Margin (kind of asking the contentView to maintain an 8 pt. padding). Hence, the conflict.
Check Visual Format syntax here.