Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From willAnimateRotationToInterfaceOrientation how can I get the size the view will be after rotation?

Tags:

iphone

I am trying to resize the objects in a UIView when the device is rotated without hard coding the width and height. In this code how would I get the newWidth and newHeight?

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    child.frame = CGRectMake(10, 10, newWidth - 20, newHeight - 20);
}
like image 628
respectTheCode Avatar asked Feb 22 '10 23:02

respectTheCode


2 Answers

This would be about right.

- (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);
}
like image 199
Kalle Avatar answered Nov 09 '22 15:11

Kalle


If possible, you're better either:

  1. Subclassing UIView and doing the layout you need inside -(void)layoutSubviews, or;
  2. Making use of autoresizingMask to automatically layout your views.
like image 43
leolobato Avatar answered Nov 09 '22 14:11

leolobato