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.

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.

WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, and free programming tips and tricks and source code            snippets.

1 Response » to “How to use Autorotation and Autosizing in iphone?”

  1. Tim says:

    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.

Leave a Reply

Security Code:

learn iphone programming

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Our Mobile Training Courses

EDUmobile.ORG offers the following 4 Mobile Training Courses. Our iPhone Training Course is very popular, with over 200 developers in training.

learn iphone programming
© 2010 EDUmobile.ORG