1
votes

To create a dark overlay on the screen, I add a view over all the other views onto UIWindow:

UIView *darkOverlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
darkOverlayView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85];

I then want to center a UIImageView in the new darkOverlayView, and try to as follows:

UIImageView *imageFromLink = [[UIImageView alloc] initWithImage:responseObject];
imageFromLink.translatesAutoresizingMaskIntoConstraints = NO;
            CGFloat widerThanHeightBy = imageFromLink.bounds.size.width / imageFromLink.bounds.size.height;

[darkOverlayView addConstraint:[NSLayoutConstraint constraintWithItem:imageFromLink attribute:NSLayoutAttributeWidth relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[UIScreen mainScreen].bounds.size.width]];
[darkOverlayView addConstraint:[NSLayoutConstraint constraintWithItem:imageFromLink attribute:NSLayoutAttributeHeight relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[UIScreen mainScreen].bounds.size.height / widerThanHeightBy]];

But every time it runs, I get the error:

The view hierarchy is not prepared for the constraint: When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug. 2013-12-07 00:59:03.626 Jupiter[58008:70b] View hierarchy unprepared for constraint. Constraint: Container hierarchy: > View not found in container hierarchy: > - (null) That view's superview: NO SUPERVIEW

How would I get around this? Or do I have to use frames?

2

2 Answers

6
votes

How say @rdelmar:

First: Where you add your imageFromLink like addSubView on to superview?

Seccond: Your constraints:

[darkOverlayView addConstraint:[NSLayoutConstraint constraintWithItem:imageFromLink attribute:NSLayoutAttributeWidth relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[UIScreen mainScreen].bounds.size.width]];
[darkOverlayView addConstraint:[NSLayoutConstraint constraintWithItem:imageFromLink attribute:NSLayoutAttributeHeight relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[UIScreen mainScreen].bounds.size.height / widerThanHeightBy]];

setup only Width and height, what about x and y

just add two constraints more:

[darkOverlayView addConstraint:[NSLayoutConstraint constraintWithItem:imageFromLink attribute:NSLayoutAttributeCenterX relatedBy:0 toItem:darkOverlayView attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
[darkOverlayView addConstraint:[NSLayoutConstraint constraintWithItem:imageFromLink attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:darkOverlayView attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]];

it's set position in center of darkOverlayView.

2
votes

When adding constraints to view that involves two views, the one view has to be a subview of the other, so you would need to add imageFromLink as a subview of darkOverLayView before you add the constraints. However, in this case, where you're adding a width and height constraint, those constraints should be added to imageFromLink, not to its superview. This type of fixed width or height constraint doesn't involve any other views, so it should belong to the view itself, not the superview.

Adding a height and width constraint doesn't center it in its superview however. You need to add (after making imageFromLink a subview) a centerX and centerY constraint to darkOverlayView as well.