2010-12-10 49 views
0

我有一個UIViewController在其中一個UIWebView。我希望UIWebView以橫向和縱向模式位於iPad屏幕的中心。所以,我已經這樣實施了爲什麼我的視圖無法在interfaceOrientation更改上正確定位?

// UIViewController 
// InfoGraphicView is the UIWebView 

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Overriden to allow any orientation. 
    return YES; 
} 

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
           duration:(NSTimeInterval)duration { 
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait || 
     toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 
     [self layoutPortrait]; 
    } else { 
     [self layoutLandscape]; 
    } 
} 

- (void)layoutLandscape { 
    NSLog(@"Layout Landscape"); 
    infoGraphicView.frame = CGRectMake(100, 100, 936, 700); 
} 

- (void)layoutPortrait { 
    NSLog(@"Layout Portrait"); 
    infoGraphicView.frame = CGRectMake(100, 100, 700, 936); 
} 

但是,它沒有按照我的預期行事。在上面的代碼中,我希望他的UIWebView是100px(或點或任何單位)遠離頂部和左側。但事實並非如此。在肖像模式下,它會與屏幕左上角齊平,而在橫向模式下,它似乎在左上角部分偏離屏幕。

如果我將該幀設置爲CGRectMake(-100, 100, 700, 936),那麼我將它定位在屏幕的中心,因爲我希望它是,但我不知道爲什麼。

像往常一樣,最有可能的東西很簡單,我忽略了,但我無法弄清楚。任何幫助一如既往非常感謝。

回答

1

您在infoGraphicView上設置的座標是相對於其超視圖而不是屏幕的一般。而且視圖不一定會限制他們的子視圖。此外,自動設置爲self.view的形狀將取決於Interface Builder中設置的縮放標誌。不過,我認爲默認情況下它被設置爲填滿整個屏幕。

這就是說,我認爲這個錯誤是在您使用willRotateToInterfaceOrientation:duration :.這在旋轉開始之前被調用,所以self.view具有舊尺寸(即,如果從縱向旋轉到橫向,則它仍然是縱向尺寸,反之亦然)。可能更好地鉤住willAnimateRotationToInterfaceOrientation:duration: - 然後設置正確的大小,你將在CoreAnimation塊內,這樣你的視圖將作爲旋轉動畫的一部分增長/縮小。

也值得檢查一下您在infoGraphicView上設置了哪些調整大小標記。除了您所做的任何更改之外,它們還會自動生效。所以你可能想要全部禁用它們。

1

這可能是Web視圖所在的視圖的問題。使用的座標系是視圖的超視圖。如果該視圖沒有在輪換時調整大小,那麼您會看到像這樣的意外佈局。您可以通過superview屬性訪問視圖的超級視圖;看到其框架的一種方法是使用其描述。將此行放入您的一種佈局方法中:

NSLog(@"Superview: %@", [infoGraphicView superview]); 

這應該打印出視圖的說明。

一旦你明白了,如果你想讓web視圖具有相同的佈局,你可以使用它的autoresizingMask屬性。如果設置這樣的:

infoGraphicView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

那麼該視圖會自動改變其寬度和高度,以保持頂部,左側,右側和底部邊緣相同。

相關問題