【问题标题】:How to center a UIButton after device is rotated in iOS如何在 iOS 中旋转设备后使 UIButton 居中
【发布时间】:2013-07-28 01:09:07
【问题描述】:

我使用以下代码以编程方式创建了一个 UIButton 并将其水平居中

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

当设备旋转时,它不再位于中心。我该如何解决这个问题?提前致谢。

【问题讨论】:

    标签: ios objective-c centering


    【解决方案1】:
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
        button.center = CGPointMake(self.view.center.x, 50);
    }
    

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多