2010-10-30 57 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