2014-03-13 38 views
1

我有一個簡單的設置與3次:NSLayoutConstraints到的UITextField - 錯誤

view,其具有1子視圖contentView,其具有1子視圖emailField(一個UITextField)。

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];} 

我真不明白什麼是錯的。約束設置是否正確?

+0

您可以簡單地提供所有的代碼,您在哪裏設置您的意見/子視圖和所有約束? –

回答

2

使用setTranslatesAutoresizingMaskIntoConstraints:關閉自動調整掩碼轉換爲contentViewemailField的限制。將其他約束添加到emailField以指定它應該使用的x位置widthheight

+0

這樣做。但是我仍然遇到錯誤:「, 」「, 」「, 」「, 」「, 」「 –

+0

看起來你是固定寬度並固定到超視圖的前導和尾隨?很難說只是指向視圖的指針值... – Wain

+0

刪除了尾隨約束,只留下了前導,現在它的工作沒有錯誤。謝謝 –