From willAnimateRotationToInterfaceOrientation, how can I get the size that will be displayed after rotation?

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

 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { child.frame = CGRectMake(10, 10, newWidth - 20, newHeight - 20); } 
+6
iphone
source share
4 answers

It will be 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 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:%fh:%f", size.width, size.height); } 
+10
source share

If possible, you are better off:

  • Subclass UIView and do the layout you need inside -(void)layoutSubviews , or;
  • Using autoresizingMask to automatically layout your views.
+1
source share

It is best to create a separate view as a whole and call this view in the did rotate method.

0
source share

You can set the Autosizing properties of the view from the interface constructor. This will change your mind when the rotation happens. But there may be a problem that some of the views may not fit properly.

0
source share

All Articles