iPhone Application Development : Creating Rotatable and Resizable Interfaces with Interface Builder

2/4/2012 4:20:32 PM
We’ll be using a label (UILabel) and a few buttons (UIButton) as our “study subjects” for this tutorial. Feel free to swap them out with other interface elements to see how rotation and resizing is handled across the iPhone object library.

Setting Up the Project

Begin by starting Xcode and creating a new application, SimpleSpin, using the Apple View-Based iPhone Application template. Although all our UI work will take place in Interface Builder, we’ll still need to enable interface rotation with the shouldAutorotateToInterfaceOrientation: method.

Open the implementation file for the view controller (SimpleSpinViewController.m), and then find and uncomment shouldAutorotateToInterfaceOrientation:. Because we’re not controlling the view programmatically at all, we’ll go ahead and enable all possible iPhone screen orientations by returning YES from this method.

The finished method implementation should resemble Listing 1.

Listing 1.
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
return YES;

Save the implementation file and switch to Interface Builder by opening the XIB file that defines the application’s view—SimpleSpinViewController.xib. All the rest of our work for this example will take place in this file.

Building a Flexible Interface

Creating a rotatable and resizable interface starts out like building any other iPhone interface—just drag and drop!

Using the Library (Tools, Library), drag a label (UILabel) and four buttons (UIButton) to the SimpleSpin view. Center the label at the top of the view and title it SimpleSpin. Name the buttons so you can tell them apart—Button 1, Button 2, Button 3, and Button 4. Position them below the label, as shown in Figure 1.

Figure 1. Build your rotatable application interface the same way you would any other application.

Testing Rotation

You’ve now built a simple application interface, just as you have in earlier lessons. To get an idea of what the interface looks like when rotated, click the curved arrow in the upper-right corner of the Interface Builder’s view window, as shown in Figure 2.

Figure 2. Use Interface Builder to immediately test the effects of rotating the view.

As you might expect, the reoriented view does not look “quite right.” The reason is that objects you add to the view are, by default, “anchored” by their upper-left corners. This means that no matter what the screen orientation is, they’ll keep the same distance from the top of the view to their top and from left of the view to their left side. Objects also, by default, are not allowed to resize within the view. As a result all elements have the exact same size in portrait or landscape orientations—even if they won’t fit in the view.

To fix our problem and create an iPhone-worthy interface, we need to use the Size Inspector.

Understanding Autosizing in the Size Inspector

As you’ve grown more experienced building iPhone applications, you’ve gotten accustomed to using the Interface Builder Inspectors. The Attributes and Connections Inspectors have been extremely valuable in configuring the appearance and functionality of your application. The Size Inspector (Command+3), on the other hand, has remained largely on the sidelines, occasionally called on to set the coordinates of a control but never used to enable functionality—until now.

The magic of autorotating and autoresizing views is managed entirely through the Size Inspector’s Autosizing settings, shown in Figure 3. This deceptively simple “square in a square” interface provides everything you need to tell Interface Builder where to anchor your controls and in which directions (horizontally or vertically) they can stretch.

Figure 3. The Autosizing settings control anchor and size properties for any onscreen object.

To understand how this works, imagine that the inner square represents one of your interface elements and the outer square is the view that contains the element. The lines between the inner and outer square are the anchors. When clicked, they toggle between solid and dashed lines. Solid lines are anchors that are set. This means that those distances will be maintained when the interface rotates.

Within the inner square are two double-headed arrows, representing horizontal and vertical resizing. Clicking these arrows toggles between solid and dashed lines. Solid arrows indicate that the item is allowed to resize horizontally, vertically, or both. As mentioned earlier, by default, objects are anchored on their top and left and are not allowed to resize. This configuration is visible in Figure 3.

Did you Know?

If you need a more “visual” means of understanding the autosizing controls, just look to the right of the two squares. The rectangle to the right shows an animated preview of what will happen to your control (represented as a red rectangle) when the view changes size around it. The easiest way to understand the relationship between anchors, resizing, and view size/orientation is to configure the anchors/resize arrows and then watch the preview to see the effect.

Applying Autosize Settings to the Interface

To modify our SimpleSpin interface with appropriate autosizing attributes, let’s analyze what we want to have happen for each element and translate that into anchors and resizing information.

As we work through the list, select each of the interface elements, and then open the Size Inspector (Commnd+3) and configure their anchors and resizing attributes as described here:

  • The SimpleSpin label: The label should float at the top center of the view. The distance between the top of the view and the label should be maintained. The size of the label should be maintained (Anchor:Top, Resizing: None).

  • Button 1: The button should maintain the same distance between its left side and the left side of the view, but it should be allowed to float up and down as needed. It can resize horizontally to better fit a larger horizontal space (Anchor: Left, Resizing: Horizontal).

  • Button 2: The button should maintain the same distance between its right side and the right side of the view, but it should be allowed to float up and down as needed. It can resize horizontally to better fit a larger horizontal space (Anchor: Right, Resizing: Horizontal).

  • Button 3: The button should maintain the same distance between its left side and the left side of the view, as well as its bottom and the bottom of the view. It can resize horizontally to better fit a larger horizontal space (Anchor: Left and Bottom, Resizing: Horizontal).

  • Button 4: The button should maintain the same distance between its right side and the right side of the view, as well as its bottom and the bottom of the view. It can resize horizontally to better fit a larger horizontal space (Anchor: Right and Bottom, Resizing: Horizontal).

After you’ve worked through one or two of the UI objects, you’ll realize that it took longer to describe what we needed to do than it did to do it! Once the anchors and resize settings are in place, the application is ready for rotation! Click the rotate arrow in the Interface Builder’s view window and review the result. Your view should now resize and resemble Figure 4.

Figure 4. The finished view now properly positions itself when rotated into a landscape orientation.

You can, if you choose, save the SimpleSpinViewController.xib changes, and then return to Xcode and click Build and Run to test the application in the iPhone Simulator or on your device. Because we haven’t modified anything programmatically in the view, it should behave exactly the same as what you’ve seen in Interface Builder.

- iPhone Application Development : Rotatable and Resizable Interfaces
- Mapping Well-Known Patterns onto Symbian OS : Handle–Body
- Mapping Well-Known Patterns onto Symbian OS : Adapter
- Mobile Web Apps : Loading Pages (part 3) - Going Backwards
- Mobile Web Apps : Loading Pages (part 2) - Sliding
- Mobile Web Apps : Loading Pages (part 1) - Swapping Pages & Fading with WebKit Animations
- Personalize & Secure Your iPad : Adjusting Sounds on your iPad & Personalize Your Picture Frame
- Personalize & Secure Your iPad : Changing your Lock Screen and Home Screen Wallpapers
- Using Media in XNA Game Studio : Media Enumeration
- Using Media in XNA Game Studio : What Is Media?
Video tutorials
- How To Install Windows 8

- How To Install Windows Server 2012

- How To Disable Windows 8 Metro UI

- How To Change Account Picture In Windows 8

- How To Unlock Administrator Account in Windows 8

- How To Restart, Log Off And Shutdown Windows 8

- How To Login To Skype Using A Microsoft Account

- How To Enable Aero Glass Effect In Windows 8

- How To Disable Windows Update in Windows 8

- How To Disable Windows 8 Metro UI

- How To Add Widgets To Windows 8 Lock Screen
programming4us programming4us
- First look: Apple Watch

- 10 Amazing Tools You Should Be Using with Dropbox

- Sigma 24mm f/1.4 DG HSM Art

- Canon EF11-24mm f/4L USM

- Creative Sound Blaster Roar 2

- Alienware 17 - Dell's Alienware laptops

- Smartwatch : Wellograph

- Xiaomi Redmi 2
Popular tags
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS
Top 10
- How To Unlock An Account From Active Directory (Windows Server 2008)
- How To Install A deviantArt Theme In Windows 7
- How To Install Windows Server 2012 On VirtualBox
- How To Fix Skype High CPU And Memory Usage In Windows 8
- Add 270 Additional Cleaning Options To CCleaner With CCEnhancer
- FPPT Provides More Than 2000 Free And Attractive PowerPoint Templates
- Setup Free Media Server To Stream Videos To DLNA Compatible TV, Xbox 360 & PS3 (Play Station 3)
- How To Install Android Market & Google Apps On Kindle Fire
- How To Make Ubuntu Look Like Windows 7
- How To Add A New Account in MS Outlook 2013