我從故事板中查看ViewController中的視圖。我嘗試改變其約束使用的代碼:iOS NSAutoLayoutConstraint:未設置故事板中的視圖
_viewFromStoryboard.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *w = [NSLayoutConstraint constraintWithItem:_viewFromStoryboard attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f];
NSLayoutConstraint *h = [NSLayoutConstraint constraintWithItem:_viewFromStoryboard attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0.0f];
NSLayoutConstraint *centerX = [NSLayoutConstraint constraintWithItem:_viewFromStoryboard attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];
NSLayoutConstraint *centerY = [NSLayoutConstraint constraintWithItem:_viewFromStoryboard attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];
[self.view addConstraints:@[w, h, centerX, centerY]];
[_viewFromStoryboard setNeedsUpdateConstraints];
在
viewDidLayoutSubviews
方法
,但制約因素似乎並沒有更新。
我在做什麼錯了?
你應該調用LayoutIfNeeded方法更新約束... – 2015-02-10 12:37:29
嘗試它,但不起作用 – Alterecho 2015-02-10 14:53:50
更新前和更新後調用此[self.view layoutIfNeeded]適用於我 – 2015-02-11 06:34:28