tags:

views:

540

answers:

4

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);
}
A: 

It might be best to create a separate view altogether and call that view in the did rotate method.

iAm
+1  A: 

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.
leolobato
+1  A: 

You can set the Autosizing properties of the view from the interface builder. That will resize your view when rotation happens. But there may be problem that some of the views might not fit properly.

Ideveloper
+1  A: 

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);
}
Kalle
What if you're in Landscape rotating to Portrait?
progrmr
applicationFrame is always set to the dimensions for portrait. Hence you flip them only if the device is going to a non-portrait orientation.
Kalle
Not exactly what I was looking for but this would do it.
Kevin
size is the width and height of the device after rotation. Your question above goes "In this code how would I get the newWidth and newHeight?" .. not sure what was "not exactly what you were looking for" in that. :P
Kalle