Iphone 从WillAnimateRotationInterfaceOrientation如何获得旋转后视图的大小?

Iphone 从WillAnimateRotationInterfaceOrientation如何获得旋转后视图的大小?,iphone,Iphone,当设备旋转时,我尝试在UIView中调整对象的大小,而不硬编码宽度和高度。在这段代码中,如何获得newWidth和newHeight - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { child.frame = CGRectMake(10, 10, newWidth - 20

当设备旋转时,我尝试在UIView中调整对象的大小,而不硬编码宽度和高度。在这段代码中,如何获得
newWidth
newHeight

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

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

如果可能,您最好:

  • 子类化
    UIView
    并在
    -(void)layoutSubview
    中进行所需的布局,或
  • 使用
    autoresizingMask
    自动布局视图

  • 可以从界面生成器中设置视图的自动调整大小属性。这将在发生旋转时调整视图的大小。但是可能会有一些视图不适合的问题。

    这大概是正确的

    - (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);
    }
    

    如果您在横向旋转到纵向,该怎么办?applicationFrame始终设置为纵向尺寸。因此,只有当设备将要转到非纵向方向时,才可以翻转它们。这与我要查找的不完全一样,但可以这样做。大小是设备旋转后的宽度和高度。您上面的问题是“在这段代码中,如何获得新宽度和新高度?”。。不确定其中的“不完全是你想要的”通知栏怎么样?