NSLayoutConstraints到UITextField - 错误

时间:2014-03-13 15:17:30

标签: ios objective-c autolayout nslayoutconstraint

我有一个简单的设置,有3个视图:

view,其中包含1个子视图contentView,其中包含1个子视图emailFieldUITextField)。

emailfField未正确对齐centerY。我得到的是这个错误:

"<NSLayoutConstraint:0x9e7d500 UIView:0x9e85c30.centerY == UITextField:0x9e4dbd0.centerY>",
"<NSLayoutConstraint:0x9e7cc00 UIView:0x9e85530.top == UIView:0x9e85c30.top>",
"<NSAutoresizingMaskLayoutConstraint:0x9e7f950 h=--& v=--& UITextField:0x9e4dbd0.midY == + 282.5>",
"<NSAutoresizingMaskLayoutConstraint:0x9e81e90 h=--& v=--& UIView:0x9e85c30.midY == + 240>")

我的.m文件:

- (void)viewDidLoad {
[super viewDidLoad];

[self initContentView];
[self initFirstView];
[self addConstraints];}

- (void)addConstraints {
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
                                                      attribute:NSLayoutAttributeLeading
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeLeading
                                                     multiplier:1.0
                                                       constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
                                                      attribute:NSLayoutAttributeTop
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeTop
                                                     multiplier:1.0
                                                       constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
                                                      attribute:NSLayoutAttributeTrailing
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeTrailing
                                                     multiplier:1.0
                                                       constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
                                                      attribute:NSLayoutAttributeBottom
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeBottom
                                                     multiplier:1.0
                                                       constant:0.0]];

//First View
//Email Field
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.emailField
                                                      attribute:NSLayoutAttributeCenterY
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.contentView
                                                      attribute:NSLayoutAttributeCenterY
                                                     multiplier:1.0
                                                       constant:0.0]];}

- (void)initContentView {
self.view.backgroundColor = [UIColor colorWithRed:239.0f/255.0f green:239.0f/255.0f blue:239.0f/255.0f alpha:1.0];

self.contentView = [[UIView alloc] initWithFrame:self.view.frame];
self.contentView.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.contentView];

int frameHorizontalCenter = self.contentView.frame.size.width/2;
self.funView = [[UIImageView alloc] initWithFrame:CGRectMake(frameHorizontalCenter-50, 100, 100, 100)];
self.funView.backgroundColor = [UIColor clearColor];
self.funView.image = [UIImage imageNamed:@"dog1"];
[self.contentView addSubview:self.funView];

self.descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(frameHorizontalCenter - 140, self.funView.frame.origin.y + self.funView.frame.size.height, 280, 60)];
self.descriptionLabel.font = [UIFont systemFontOfSize:14];
self.descriptionLabel.textColor = [UIColor colorWithRed:63.0f/255.0f green:63.0f/255.0f blue:63.0f/255.0f alpha:1.0];
self.descriptionLabel.backgroundColor = [UIColor clearColor];
self.descriptionLabel.highlightedTextColor = [UIColor whiteColor];
self.descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.descriptionLabel.numberOfLines = 2;
self.descriptionLabel.adjustsFontSizeToFitWidth = YES;
self.descriptionLabel.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:self.descriptionLabel];

self.emailField = [[UITextField alloc] initWithFrame:CGRectMake(frameHorizontalCenter -140, self.descriptionLabel.frame.origin.y + self.descriptionLabel.frame.size.height, 280, 45)];
self.emailField.borderStyle = UITextBorderStyleNone;
self.emailField.textColor = [UIColor colorWithRed:63.0f/255.0f green:63.0f/255.0f blue:63.0f/255.0f alpha:1.0];
self.emailField.backgroundColor = [UIColor whiteColor];
self.emailField.placeholder = @"Epasts";
[self.contentView addSubview:self.emailField];}

我真的不明白什么是错的。约束设置不正确吗?

1 个答案:

答案 0 :(得分:2)

使用setTranslatesAutoresizingMaskIntoConstraints:关闭自动调整屏蔽转换为contentViewemailField上的约束。向emailField添加其他约束,以指定应使用的x位置,widthheight