2010-10-30 48 views
1

我的應用程序中有一個UIImageView內的UIImage。在旋轉後自動居中UIImageView

在縱向模式下,圖像居中,但當我切換到橫向模式時,它仍然位於左側。

所以我在我的.m文件中添加了以下方法,但問題是我有一個TabBar應用程序,所以當我在另一個選項卡上旋轉設備並返回包含圖像的選項卡時,自動旋轉。

有一種方法可以在任何選項卡上旋轉設備時自動旋轉應用程序的所有元素?


-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
    if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)){ 
     background.image = [UIImage imageNamed:@"back2-landscape.png"]; 
    } else if((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)){ 
     background.image = [UIImage imageNamed:@"back2-portrait.png"]; 
    } 
} 

謝謝:-)

回答

7

有兩種選擇:

* 1。在IB中進行操作 - 將關閉「尺寸檢查器」下「自動調整」區域中的所有這些小空格和箭頭。你應該能夠「模擬接口」,並旋轉[模擬]設備,對象居中。

alt text

* 2。以編程方式執行,在didRotateFromInterfaceOrientation中執行下列操作:

[theObject setCenter:self.center]; 
+0

我使用第一種方法做了它,並且它完美地工作。非常感謝你 :-) – bnabilos 2010-10-31 01:36:29