为什么我的编程创建的视图忽略了它的约束?

时间:2021-03-09 20:21:33

I have a view that programmatically creates a subview and adds some constraints to it. For some reason, the constraints are being completely ignored. No error is thrown. The constraints simply do not take effect. The subview has the same frame as it's superview. I'm expecting it to be 400x400 and centered in its superview. What am I doing wrong here? Here's my initWithFrame in the superview:

我有一个以编程方式创建子视图并向其添加一些约束的视图。由于某些原因,约束被完全忽略。不抛出错误。这些限制根本不起作用。子视图的框架与父视图相同。我希望它是400x400,在它的父视图中居中。我在这里做错了什么?这是我的initWithFrame在superview:

- (id) initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        instructionsView = [[UIView alloc] initWithFrame:frame];
        [self addSubview:instructionsView];
        [self bringSubviewToFront:instructionsView];

        [self addConstraint:[NSLayoutConstraint constraintWithItem:instructionsView
                                                         attribute:NSLayoutAttributeCenterX
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self
                                                         attribute:NSLayoutAttributeCenterX
                                                        multiplier:1.
                                                          constant:0]];

        [self addConstraint:[NSLayoutConstraint constraintWithItem:instructionsView
                                                         attribute:NSLayoutAttributeCenterY
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self
                                                         attribute:NSLayoutAttributeCenterY
                                                        multiplier:1.
                                                          constant:0]];

        [instructionsView addConstraint:[NSLayoutConstraint constraintWithItem:instructionsView
                                                                     attribute:NSLayoutAttributeWidth
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:nil
                                                                     attribute:NSLayoutAttributeNotAnAttribute
                                                                    multiplier:1.
                                                                      constant:400]];

        [instructionsView addConstraint:[NSLayoutConstraint constraintWithItem:instructionsView
                                                                     attribute:NSLayoutAttributeHeight
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:nil
                                                                     attribute:NSLayoutAttributeNotAnAttribute
                                                                    multiplier:1.
                                                                      constant:400]];
    }
    return self;
}

I have tried changing

我已经尝试改变

instructionsView = [[UIView alloc] initWithFrame:frame];

to

instructionsView = [[UIView alloc] initWithFrame:CGRectZero];

with no success.

没有成功。

2 个解决方案

#1


9  

You need to tell the view not to use autoresizing mask based layout and instead use autolayout

您需要告诉视图不要使用基于自定义的掩码,而是使用autolayout。

instructionsView.translatesAutoresizingMaskIntoConstraints = NO;

See the documentation here

在这里看到的文档

Also, the view should be created without a frame, this is mostly a style thing but create your view like this:

此外,视图应该不需要框架来创建,这主要是一种样式,但创建您的视图如下:

instructionsView = [[UIView alloc] init];

#2


2  

Ambiguity can cause surprising results without raising any exception. To debug constraints for ambiguity, pause in the debugger and type at the lldb command line:

歧义会导致令人惊讶的结果,而不会引发任何异常。要调试模糊限制,请暂停调试器,并在lldb命令行输入:

po [[UIWindow keyWindow] _autolayoutTrace]

Also, you may (and should) add a method (through a category) on UIView that lets you call hasAmbiguousLayout recursively down the hierarchy.

此外,您可以(也应该)在UIView上添加一个方法(通过一个类别),让您在层次结构中递归地调用has歧义布局。

Finally, you may (and should) add a method (through a category) on UIView that lets you call constraintsAffectingLayoutForOrientation: recursively down the hierarchy.

最后,您可以(也应该)在UIView上添加一个方法(通过一个类别),该方法允许您调用constraintsAffectingLayoutForOrientation:递归地向下调用层次结构。

In this way you can track down what's really happening.

这样你就可以追踪到真正发生了什么。

#1


9  

You need to tell the view not to use autoresizing mask based layout and instead use autolayout

您需要告诉视图不要使用基于自定义的掩码,而是使用autolayout。

instructionsView.translatesAutoresizingMaskIntoConstraints = NO;

See the documentation here

在这里看到的文档

Also, the view should be created without a frame, this is mostly a style thing but create your view like this:

此外,视图应该不需要框架来创建,这主要是一种样式,但创建您的视图如下:

instructionsView = [[UIView alloc] init];

#2


2  

Ambiguity can cause surprising results without raising any exception. To debug constraints for ambiguity, pause in the debugger and type at the lldb command line:

歧义会导致令人惊讶的结果,而不会引发任何异常。要调试模糊限制,请暂停调试器,并在lldb命令行输入:

po [[UIWindow keyWindow] _autolayoutTrace]

Also, you may (and should) add a method (through a category) on UIView that lets you call hasAmbiguousLayout recursively down the hierarchy.

此外,您可以(也应该)在UIView上添加一个方法(通过一个类别),让您在层次结构中递归地调用has歧义布局。

Finally, you may (and should) add a method (through a category) on UIView that lets you call constraintsAffectingLayoutForOrientation: recursively down the hierarchy.

最后,您可以(也应该)在UIView上添加一个方法(通过一个类别),该方法允许您调用constraintsAffectingLayoutForOrientation:递归地向下调用层次结构。

In this way you can track down what's really happening.

这样你就可以追踪到真正发生了什么。