views:

40

answers:

2

hi everybody. in my app all my controls are created in code. i have not used IB for controls. now i want to rotate the view to landscape mode. i know i have to use the shouldAutorotate method for this purpose. but since i have not used IB, how can i resize the controls when they are in landscape mode? how can i position them correctly using code only??? plz help me, it is kind of important and urgent. thnx in advance.

A: 

You position them correctly using code the same way you likely positioned them in the original orientation, for example by setting their frame.

As for where in code to do this, check out the various orientation change methods of UIViewController that will be called when the device orientation changes. You could, for example, move/resize the controls in the view within willAnimateRotationToInterfaceOrientation:duration:.

imaginaryboy
hi. thnx for the response. but my concern is do i have to set the frame for every object when its in landscape? i mean will i have to set the frame for every object twice, ie- in portrait and in landscape? that will become little cumbersome, since i have lots of views and lots of controls in each of them? do u have any sample example or code which will help me?
Jayshree
+1  A: 

In most cases you can get views to resize themselves appropriately just by setting their autoresizingMask property to some combination of:

  • UIViewAutoresizingFlexibleLeftMargin
  • UIViewAutoresizingFlexibleRightMargin
  • UIViewAutoresizingFlexibleTopMargin
  • UIViewAutoresizingFlexibleBottomMargin
  • UIViewAutoresizingFlexibleWidth
  • UIViewAutoresizingFlexibleHeight

For example, let's say you want a view's width to increase when you rotate it to landscape, you want it to maintain the same margins relative to the top, left, and right sides of the screen, and you want its height to remain the same. This means that out of the six attributes above, only the width and bottom margin should be flexible. The other four attributes are fixed. So you would do:

yourView.autoresizingMask = UIViewAutoResizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;

In those rare cases when you can't get the views to behave appropriately using their autoresizingMask property, you can wrap them in a custom view and override that view's layoutSubviews method. This method gets called when the view's frame changes due to autorotation, so you can check [[UIApplication sharedApplication] statusBarOrientation] and update the frames of your subviews manually.

cduhn
hey thnx. i think this will help me with wat i am trying to achieve. i thnk the autoresizing properties will workout well.
Jayshree