2014-09-21 35 views
0

我创建的界面生成器为UIView的高度约束,设置高度为=的上海华。我将乘数设置为超视图宽度的1/3。这很好,它将iPhone 5/5S上的高度设置为〜107pts。力量倍增器来更新Interface Builder的约束

对于较宽的屏幕,我想设置乘数1/4代替;我想我可以用Size Classes来做到这一点,但我不确定这是最好的方法吗?

在我的视图控制器的viewWillAppear方法,我试着做以下;不管我做什么,它仍然只将高度设置为~107pts。请注意,_controlsAspectRatio是IB约束的IBOutlet

_controlsAspectRatio = [NSLayoutConstraint constraintWithItem:_controlsAspectRatio.firstItem attribute:_controlsAspectRatio.firstAttribute relatedBy:_controlsAspectRatio.relation toItem:_controlsAspectRatio.secondItem attribute:_controlsAspectRatio.secondAttribute multiplier:1.0f/4.0f constant:0]; 

// I've tried several combinations of several layout refresh options, to no avail 
[_controlsContainer setNeedsUpdateConstraints]; 
[_controlsContainer setNeedsLayout]; 
[_controlsContainer layoutIfNeeded]; 
NSLog(@"%0.4f - %0.4f", _controlsContainer.frame.size.height, _controlsAspectRatio.multiplier); 

// Output: 106.6667 - 0 

回答

0

不得不删除原来的,并添加一个完全新的约束:

NSLayoutConstraint* newControlsAspectRatio = [NSLayoutConstraint constraintWithItem:_controlsAspectRatio.firstItem attribute:_controlsAspectRatio.firstAttribute relatedBy:_controlsAspectRatio.relation toItem:_controlsAspectRatio.secondItem attribute:_controlsAspectRatio.secondAttribute multiplier:1.0f/4.0f constant:0]; 
[self.view removeConstraint:_controlsAspectRatio]; 
[self.view addConstraint:newControlsAspectRatio]; 
[_controlsContainer layoutIfNeeded]; 
NSLog(@"%0.4f - %0.4f", _controlsContainer.frame.size.height, newControlsAspectRatio.multiplier);