【发布时间】:2015-02-10 12:21:29
【问题描述】:
我的 ViewController 中有来自情节提要的视图。我尝试使用代码更改它的约束:
_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 方法来更新约束...
-
试过了,还是不行
-
更新前和更新后调用此 [self.view layoutIfNeeded] 对我有用
标签: ios cocoa-touch autolayout