0

I have created a UIButton programmatically and horizontally centered it using the following code

    button.center = CGPointMake(self.view.center.x, 50);

When device is rotated, it's no longer in the center. How can I fix this problem? Thanks in advance.

4

2 回答 2

1

Kevin 是正确的,但更好的解决方案是在视图控制器的willAnimateRotationToInterfaceOrientation:duration:方法中设置按钮的中心。

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
    button.center = CGPointMake(self.view.center.x, 50);
}

他使用了这种didRotateFromInterfaceOrientation:fromInterfaceOrientation 方法,正如 Apple 文档所述,“这种方法可能用于重新启用视图交互、重新开始媒体播放或打开昂贵的绘图或实时更新。

在布置子视图的情况下,willAnimateRotationToInterfaceOrientation:duration:将提供更平滑的过渡,因为它是从旋转的动画块中调用的。

于 2013-07-28T00:35:24.613 回答
1
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    button.center = CGPointMake(self.view.center.x, 50);
}
于 2013-07-28T00:24:55.867 回答