限制可以這樣的方式進行,即當超視圖邊界發生變化時,視圖的位置和大小可以自動更改(不檢查方向)。如果以這種方式進行約束,則無論視圖在旋轉時是否在屏幕上,視圖都將具有適當的旋轉佈局。
限制的形式爲y = m*x + b
其中y和x是兩個視圖,m是乘數,b是常數。它需要做一些數學計算m和b的值會給你什麼樣的約束。我在NSLayoutConstraint上做了一個類,它允許你直接指定你想要的肖像和風景的值。您可以使用它像這樣,
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addConstraint:[NSLayoutConstraint widthConstraintForView:self.rectangle superview:self.view portraitValue:100 landscapeValue:200]];
[self.view addConstraint:[NSLayoutConstraint heightConstraintForView:self.rectangle superview:self.view portraitValue:150 landscapeValue:80]];
[self.view addConstraint:[NSLayoutConstraint topConstraintForView:self.rectangle viewAttribute:NSLayoutAttributeTop superview:self.view portraitValue:200 landscapeValue:10]];
[self.view addConstraint:[NSLayoutConstraint leftConstraintForView:self.rectangle viewAttribute:NSLayoutAttributeLeft superview:self.view portraitValue:100 landscapeValue:100]];
}
如果您有任何IB設置的限制將被這些所取代,你可以將它們標記爲將在運行時被刪除的佔位符。類別看起來是這樣的,
+(NSLayoutConstraint *)heightConstraintForView:(UIView *)subview superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
CGFloat multiplier = (pValue - lValue)/(superview.bounds.size.height - superview.bounds.size.width);
CGFloat constant = pValue - (superview.bounds.size.height * multiplier);
NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeHeight relatedBy:0 toItem:superview attribute:NSLayoutAttributeHeight multiplier:multiplier constant:constant];
NSLog(@"height coeffs: %f %f",multiplier,constant);
return con;
}
+(NSLayoutConstraint *)widthConstraintForView:(UIView *)subview superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
CGFloat multiplier = (pValue - lValue)/(superview.bounds.size.width - superview.bounds.size.height);
CGFloat constant = pValue - (superview.bounds.size.width * multiplier);
NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeWidth relatedBy:0 toItem:superview attribute:NSLayoutAttributeWidth multiplier:multiplier constant:constant];
NSLog(@"width coeffs: %f %f",multiplier,constant);
return con;
}
+(NSLayoutConstraint *)leftConstraintForView:(UIView *)subview viewAttribute:(NSLayoutAttribute) att superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
CGFloat multiplier = (pValue - lValue)/(superview.bounds.size.width - superview.bounds.size.height);
CGFloat constant = pValue - (superview.bounds.size.width * multiplier);
NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:att relatedBy:0 toItem:superview attribute:NSLayoutAttributeRight multiplier:multiplier constant:constant];
NSLog(@"left coeffs: %f %f",multiplier,constant);
return con;
}
+(NSLayoutConstraint *)rightConstraintForView:(UIView *)subview viewAttribute:(NSLayoutAttribute) att superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
CGFloat multiplier = (superview.bounds.size.width - pValue - superview.bounds.size.height + lValue)/(superview.bounds.size.width - superview.bounds.size.height);
CGFloat constant = superview.bounds.size.width - pValue - (superview.bounds.size.width * multiplier);
NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:att relatedBy:0 toItem:superview attribute:NSLayoutAttributeRight multiplier:multiplier constant:constant];
NSLog(@"right coeffs: %f %f",multiplier,constant);
return con;
}
+(NSLayoutConstraint *)topConstraintForView:(UIView *)subview viewAttribute:(NSLayoutAttribute) att superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
CGFloat multiplier = (pValue - lValue)/(superview.bounds.size.height - superview.bounds.size.width);
CGFloat constant = pValue - (superview.bounds.size.height * multiplier);
NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:att relatedBy:0 toItem:superview attribute:NSLayoutAttributeBottom multiplier:multiplier constant:constant];
NSLog(@"top coeffs: %f %f",multiplier,constant);
return con;
}
+(NSLayoutConstraint *)bottomConstraintForView:(UIView *)subview viewAttribute:(NSLayoutAttribute) att superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
CGFloat multiplier = (superview.bounds.size.height - pValue - superview.bounds.size.width + lValue)/(superview.bounds.size.height - superview.bounds.size.width);
CGFloat constant = superview.bounds.size.height - pValue - (superview.bounds.size.height * multiplier);
NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:att relatedBy:0 toItem:superview attribute:NSLayoutAttributeBottom multiplier:multiplier constant:constant];
NSLog(@"bottom coeffs: %f %f",multiplier,constant);
return con;
}
我有這樣的例子項目張貼在這裏,http://jmp.sh/SYgejs6
你也想這一轉變過程中,如果你旋轉,而viewControllerA在屏幕上發生的呢? – rdelmar
@ rdelmar - 是的!當我在屏幕上旋轉viewControllerA時,它會執行它應該的。 –
如果您設置約束條件以使視圖在旋轉時自動調整,則無論是否在屏幕上都會執行該約束。 – rdelmar