2014-06-30 47 views
1

我有一種情況,viewControllerAviewControllerB推到導航堆棧上。當用戶旋轉屏幕並且viewControllerB的方向改變時,我想要subviewAviewControllerA進行轉換並重新定位它自己。這可能嗎?當屏幕方向改變時,可能在前一個視圖控制器上轉換視圖?

我試過將subviewA的指針發送到viewControllerB,但是我對它的位置所做的任何操作都會被忽略(當屏幕上顯示當前的viewControllerB)。

我也試過之後viewControllerB離開本層重新定位在viewControllerAviewDidAppearsubviewA但它看起來醜陋,因爲它很快重新定位自身,因爲它涉及在屏幕上。也嘗試在viewWillAppear中操縱它,但沒有任何反應。

我能想到的唯一的事情就是viewControllerA的整個self.view發送到viewControllerB並附它作爲一個子視圖時viewControllerB旋轉,然後操縱它,然後將其刪除。但顯然這是一個可怕的解決方案。

有沒有辦法做到這一點?

+1

你也想這一轉變過程中,如果你旋轉,而viewControllerA在屏幕上發生的呢? – rdelmar

+0

@ rdelmar - 是的!當我在屏幕上旋轉viewControllerA時,它會執行它應該的。 –

+0

如果您設置約束條件以使視圖在旋轉時自動調整,則無論是否在屏幕上都會執行該約束。 – rdelmar

回答

2

限制可以這樣的方式進行,即當超視圖邊界發生變化時,視圖的位置和大小可以自動更改(不檢查方向)。如果以這種方式進行約束,則無論視圖在旋轉時是否在屏幕上,視圖都將具有適當的旋轉佈局。

限制的形式爲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

相關問題