2010-10-29 52 views
3

在UIView內部旋轉滾動視圖時,滾動視圖不會使用其默認的自動修復行爲正確定位。在旋轉UIView時覆蓋layoutSubviews

因此,當發生旋轉時,在willRotateToInterfaceOrientation我打電話[self.view setNeedsLayout];layoutSubviews方法如下:

- (void)layoutSubviews { 
    NSLog(@"here in layoutSubviews"); 
} 

但把一個斷點的方法,並且它似乎永遠不會進入方法。

我需要做些其他的事情才能使它起作用嗎?

謝謝。

回答

3

willRotateToInterfaceOrientation在方向改變之前被調用,因此你的UIView將仍然具有舊的尺寸。 請嘗試改用didRotateFromInterfaceOrientation。另外,爲了增加效果,我會在willRotateToInterfaceOrientation中隱藏scrollView(也許在UIAnimation塊內),調整它的大小,然後在didRotateFromInterfaceOrientation(再次,也許在動畫塊內)顯示它。

這裏是我的應用程序的一個片段:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.3f]; 
self.myScroll.hidden = YES; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
[UIView commitAnimations]; 
} 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.3f]; 
self.myScroll.hidden = NO; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
[UIView commitAnimations]; 
} 

,你甚至可以通過檢查使用UIInterfaceOrientationIsPortrait(方向)或UIInterfaceOrientationIsLandscape(方向)的新方向做花哨的東西。

0

其實,你可能要重寫視圖控制器willAnimateRotationToInterfaceOrientation:時間:方法(切從礦爲例):

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
             duration:(NSTimeInterval)duration { 
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) { 
     boardView.frame = CGRectMake(0, 0, 320, 320); 
     buttonsView.frame = CGRectMake(0, 320, 320, 140); 
    } else { 
     boardView.frame = CGRectMake(0, 0, 300, 300); 
     buttonsView.frame = CGRectMake(300, 0, 180, 300);   
    } 

}

0

的方法不會被調用,因爲視圖控制器沒有layoutSubviews方法。

當您撥打[self.view setNeedsLayout];時,它只會調用視圖控制器視圖的layoutSubviews方法:[self.view layoutSubviews]

您需要繼承UIScrollview才能完成此工作。

2

調用[someScrollView setNeedsLayout]如果視圖未被調整大小或移動,可能實際上不會做任何事情。正如你所說,從來沒有使用默認的自動調整行爲調用該方法,因爲默認行爲是根本不調整大小。您最有可能需要設置someScrollView.autoresizingMask。當界面旋轉時,視圖將自行調整大小,並且將調用layoutSubviews

0

您應該重寫willAnimate ...並設置視圖的新框架。在輪換期間應該自動調用Layoutsubviews。