How To Use Autorotation And Autosizing In iPhone?

This application used both Portrait and Landscape mode. I will show you how to change that orientation at runtime.

Step 1: Start a new project in Xcode , and call it Autosize. We’re going to stick with the same view-based application template for this application.

Step 2: Open the ViewController file , there you’ll see that a method called shouldAutorotateToInterfaceOrientation: is already provided for you courtesy of the template. The method should look like this right now:


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)i nterfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait||
interfaceOrientation == UIInterfaceOrientationLandscapeLeft||
interfaceOrientation == UIInterfaceOrientationLandscapeRight);




Step 3:  Double click .xib file to open the file in Interface Builder. Once nice thing about using autosize attributes is that they require very little code. Now drag six Round Rect Buttons from the library over to your view, and place them. Double click each button , and assign a title to each one. We’ve numbered ours fron 1 to 6.

Step 4: Single click top left  button on your view, and bring up size inspector. The size inspector allows you to set an object’s autosize attributes .

Step 5: After doing that, save and go to the Xcode. click Build and Go again to launch the iPhone Simulator. Your screens should look something like this:

Figure 2: Image look like in Portrait Mode

Step 6:  If you select Hardware >Rotated left then ,Your screens should look something like this:

Figure 3: The buttons in their new positions after rotating.

Leave a Comment:

1 comment
Tim says April 11, 2011

Great post, this was troubling me. Suffice it to say this should be the default behavior! I’ll change my template files thanks to your help.

Add Your Reply