Ios 为什么在向添加到UIWindow的视图添加约束时无法设置自动布局约束?

Ios 为什么在向添加到UIWindow的视图添加约束时无法设置自动布局约束?,ios,objective-c,uiimageview,autolayout,nslayoutconstraint,Ios,Objective C,Uiimageview,Autolayout,Nslayoutconstraint,为了在屏幕上创建一个黑色覆盖,我在UIWindow中添加了一个覆盖所有其他视图的视图: UIView *darkOverlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; darkOverlayView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85]; 然后,我想在新的darkOverlayView中居中放置UII

为了在屏幕上创建一个黑色覆盖,我在UIWindow中添加了一个覆盖所有其他视图的视图:

UIView *darkOverlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
darkOverlayView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85];
然后,我想在新的
darkOverlayView
中居中放置UIImageView,并尝试如下操作:

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]];
但每次它运行时,我都会得到错误:

未为约束准备视图层次: 添加到视图时,约束项必须是该视图(或视图本身)的后代。如果在装配视图层次之前需要解决约束,则此操作将崩溃。中断-[UIView\u viewHierarchyUnpreparedForConstraint:]以进行调试。 2013-12-07 00:59:03.626 Jupiter[58008:70b]视图层次未准备好约束。 约束条件: 容器层次结构: > 在容器层次结构中找不到视图:>-(null) 该视图是superview:无superview


我该怎么解决这个问题?还是必须使用框架?

当向包含两个视图的视图添加约束时,一个视图必须是另一个视图的子视图,因此在添加约束之前,需要将imageFromLink添加为darkOverLayView的子视图。但是,在本例中,如果要添加宽度和高度约束,则应将这些约束添加到imageFromLink,而不是其superview。这种类型的固定宽度或高度约束不涉及任何其他视图,因此它应该属于视图本身,而不是超级视图


但是,添加高度和宽度约束不会使其在superview中居中。您还需要向darkOverlayView添加(将imageFromLink设置为子视图后)centerX和centerY约束。

向包含两个视图的视图添加约束时,一个视图必须是另一个视图的子视图,因此在添加约束之前,您需要将imageFromLink添加为darkOverlayView的子视图。但是,在本例中,如果要添加宽度和高度约束,则应将这些约束添加到imageFromLink,而不是其superview。这种类型的固定宽度或高度约束不涉及任何其他视图,因此它应该属于视图本身,而不是超级视图

但是,添加高度和宽度约束不会使其在superview中居中。您还需要(在将imageFromLink设置为子视图后)将centerX和centerY约束添加到darkOverlayView。

如何说@rdelmar:

第一:在哪里将您的
imageFromLink
像addSubView一样添加到superview

秒: 您的限制:

[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]];
只设置
宽度
高度
,那么
x
y

只需再添加两个约束:

[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]];
它位于darkOverlayView的中心位置。

怎么说@rdelmar:

第一:在哪里将您的
imageFromLink
像addSubView一样添加到superview

秒: 您的限制:

[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]];
只设置
宽度
高度
,那么
x
y

只需再添加两个约束:

[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]];
它位于darkOverlayView的中心位置