2014-09-21 38 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);