从willAnimateRotationToInterfaceOrientation我怎样才能获得旋转后视图的大小?

时间:2010-02-22 23:48:48

标签: iphone

我正在尝试在旋转设备时调整UIView中的对象,而不对硬编码宽度和高度进行硬编码。在此代码中,我如何获得newWidthnewHeight

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    child.frame = CGRectMake(10, 10, newWidth - 20, newHeight - 20);
}

4 个答案:

答案 0 :(得分:10)

这是正确的。

- (void)willAnimateRotationToInterfaceOrientation:
    (UIInterfaceOrientation)toInterfaceOrientation
    duration:(NSTimeInterval)duration
{
    // we grab the screen frame first off; these are always
    // in portrait mode
    CGRect bounds = [[UIScreen mainScreen] applicationFrame];
    CGSize size = bounds.size;

    // let's figure out if width/height must be swapped
    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
        // we're going to landscape, which means we gotta swap them
        size.width = bounds.size.height;
        size.height = bounds.size.width;
    }
    // size is now the width and height that we will have after the rotation
    NSLog(@"size: w:%f h:%f", size.width, size.height);
}

答案 1 :(得分:1)

如果可能,你也会更好:

  1. UIView进行子类化并在-(void)layoutSubviews内进行所需的布局,或者
  2. 利用autoresizingMask自动布置视图。

答案 2 :(得分:0)

最好完全创建一个单独的视图,并在did rotate方法中调用该视图。

答案 3 :(得分:0)

您可以从界面构建器设置视图的自动调整大小属性。这将在旋转发生时调整视图大小。但是可能存在某些观点可能不合适的问题。