我有一个视图,可以以编程方式创建一个子视图并为其添加一些约束。由于某些原因,这些约束已被完全忽略。没有引发任何错误。约束根本不会生效。子视图与父视图具有相同的框架。我希望它是400x400,并以其超级视图为中心。我在这里做错了什么?这是我在超级视图中的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];
没有成功。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句