旋转后自动居中UIImageView

bna*_*los 1 iphone

我在我的应用程序中的UIImageView中有一个UIImage.

在纵向模式下,图像居中,但当我切换到横向模式时,它仍然在左侧.

所以我在我的.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"];
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢 :-)

Bra*_*rad 7

有两种选择:

*1.在IB中进行操作 - 关闭 "尺寸检查器"下"自动调整"区域中的所有小空格和箭头.您应该能够"模拟界面",并旋转[模拟]设备,对象保持居中.

替代文字

*2.以编程方式,在didRotateFromInterfaceOrientation中执行以下操作:

[theObject setCenter:self.center];
Run Code Online (Sandbox Code Playgroud)