我有一个程序创建一个子视图,并增加了一些约束对它的看法。出于某种原因,这些约束被完全忽略。没有错误被抛出。约束根本不起作用。该子视图具有相同的框架,因为它是上海华。我期待它是400×400,并在它的父居中。我在这里做错了什么?这是我在上海华initWithFrame:方法为什么我的编程方式创建视图忽略它的约束?
- (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;
}
我试图改变
instructionsView = [[UIView alloc] initWithFrame:frame];
到
instructionsView = [[UIView alloc] initWithFrame:CGRectZero];
没有成功。