IT tutorials
 
Mobile
 

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

2/4/2012 4:20:32 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
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.

 
Others
 
- 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?
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us