TweetFollow Us on Twitter

Interface Builder Tips and Tricks

Volume Number: 19 (2003)
Issue Number: 10
Column Tag: Programming

Mac OS X Programming Secrets

Interface Builder Tips and Tricks

by Scott Knaster

When you're programming with Cocoa, you spend plenty of time in Interface Builder, instantiating objects, positioning things just right in windows, and making connections. In this month's column, we're going to shine the light of day on a few valuable tips and shortcuts for making yourself more productive when using Interface Builder.

Designing for Success

Most of the intricate work in Interface Builder is in the design window, which is where you add objects from palettes, lay them out, resize them, and so on. In this section, we'll discuss some tips for use in design windows.

The first trick is useful for figuring out precise distances between objects while looking at them in the design window. If you select an object, then hold down the Option key, you'll see the exact distances from the borders of the object to the edges of the window (see Figure 1).


Figure 1. Hold down Option to see distances.

But wait, that's not all. By moving the mouse pointer while holding down Option, you can have Interface Builder show you the distance between objects, rather than just the distance to the window edges. In Figure 2, the pointer is on the static text object, and the window shows the distances from the selection to each edge of that object.


Figure 2. Hold down Option to see distances.

You can use another Interface Builder feature to make objects automatically resize or reposition themselves when you change the size of their enclosing window. For example, let's say you want to preserve the proportional distance between the three buttons in the window above: if you widen the window, you want the distance between the buttons to increase. To make this happen, you can create springs between the objects. To do this, select each object in turn, open the size inspector, and click that line that you want to become a spring (a proportional relationship that changes automatically when you resize the window). Figure 3 shows what this looks like for the "Stop" button in the window above: we've clicked on its connection lines to make them into springs. We'll do the same for all 3 buttons.

Now, when we resize the window, if we hold down the Control key, the springs will cause the distances between objects to change proportionally. If we want the springs to be ignored, we can just resize without holding down Control.


Figure 3. Stop button has springs on its left and right.

Speaking of the size inspector, it's even smarter than you might have realized. You can type math expressions into the coordinate fields and let Interface Builder evaluate them for you. For example, if you want to add 32 pixels to an object's width, you can just click in the w field, select after the current value, then type "+32" and press return. Neato! Apple even provides code to help you implement this trick in your own fields. Take a look at /Developer/Examples/InterfaceBuilder/BusyPalette for details.

Another fun trick you can use with design windows is to see all the rectangles that Interface Builder uses to determine distances between objects. By using the Layout --> Show Layout Rectangles menu item, or just pressing command-L, you can see all the layout rectangles at once, as shown in Figure 4.


Figure 4. Design window with all layout rectangles shown.

Interface Builder tips aren't all about layout. For example, there's a quick way to set the tab order for objects in the window. You'll want to do this to allow users to tab from one text field to the next. The tab order starts with the initial first responder, and continues from there by following each object's nextKeyView field.

To start setting the order, select the object you want to be the initial first responder and choose Layout --> Keyboard Navigation --> Make Initial First Responder. Then, to set the tab order, choose Layout --> Keyboard Navigation --> Show Keyboard Check, Control-drag from one object to the next, then click Connect in the connection inspector. That will set the tab connection between the objects. Note that when you have keyboard check turned on, the word tab appears at the bottom left of the window. That reminds you that you can press tab to test the tab order.

Cool Stuff in Other Windows, Too

Not all the neat tricks are in the design window - here are a couple you can use in the document window. If you click the tiny control on the right side of the window, the object instances are displayed in an outline view, with the number of connections, both into and out of the object. If you click on one of these numbers, the connections appear in the window, as you can see in Figure 5.


Figure 5. Outline view shows connections to an object.

You can use this view to delete connections between objects. If you hold down Control and point at a line that connects two objects, the pointer changes to a tiny pair of scissors, and clicking (you guessed it) cuts the connection.

If you just want to learn more about the connected objects, click on the name of the connected field, such as menu or _panel in Figure 5. The connected objects will appear on the screen with a line between them.

There's More (Always)

You can find more fun and timesaving tips like these by reading the informative FAQ that comes with Interface Builder. Get to the FAQ by choosing FAQ in Interface Builder's Help menu. There's even a sense of humor in the FAQ: in the section on modifier keys, right after the descriptions of what happens if you press Command, Option, and Control, the FAQ tells you that you can use the Left Pedal to disengage the clutch. Cool.


Scott Knaster has been writing about Macs for as long as there have been Macs. Scott's books How To Write Macintosh Software and Macintosh Programming Secrets were required reading for Mac programmers for more than a decade. Scott wrote developer books for General Magic and worked on Mac software for Microsoft. Scott's books have been translated into Japanese and Pascal. Scott has every issue of Mad magazine, which explains a lot.

 
AAPL
$562.29
Apple Inc.
-3.03
MSFT
$29.06
Microsoft Corpora
-0.01
GOOG
$591.53
Google Inc.
-12.13
MacTech Search:
Community Search:

SketchBook Ink Review
SketchBook Ink Review By Lisa Caplan on May 25th, 2012 Our Rating: :: SIMPLEiPad Only App - Designed for the iPad SketchBook Ink has a welcoming interface but lacks key features   Developer: Autodesk Inc. | Read more »
Autumn Dynasty Review
Autumn Dynasty Review By Kevin Stout on May 25th, 2012 Our Rating: :: NEARLY FLAWLESSiPad Only App - Designed for the iPad Autumn Dynasty is an oriental-themed real-time strategy game.   | Read more »
Our Annual “Holy Cow It’s Memorial Day A...
So, it’s that time of year again! BBQs, lawn chairs, beer, and the ability to finally wear shorts with sandals without fear of frostbite. Tan those legs and check out all the huge sales that are going on across the App Store below. We’ll try and... | Read more »
FREEday 5/25/12 – “They Call Me FREE but...
Another week of freebies, this time with very little in the way of “Big Name” titles. No need to panic, it’s intentional. Anyone browsing the App Store will no doubt see the more popular games anyway. | Read more »
Shoot the Zombirds Review
Shoot the Zombirds Review By Kevin Stout on May 25th, 2012 Our Rating: :: ADDICTINGUniversal App - Designed for iPhone and iPad Shoot the Zombirds is an archery game where the player shoots arrows at avian zombies.   | Read more »
Apple Debuts Free App of the Week Promot...
Apple has made a couple of changes to their weekly app features that pop up in the Featured tab of the App Store. While “App of the Week” and “Game of the Week” appear to be just rebranded as “Editors’ Choice,” there’s a new feature: the Free Game... | Read more »
Gun Runner Review
Gun Runner Review By Jason Wadsworth on May 25th, 2012 Our Rating: :: RUN AND GUNUniversal App - Designed for iPhone and iPad The name says it all. This clever homage to classic side-scrolling shooters is easy to enjoy but hard to... | Read more »

Price Scanner via MacPrices.net

Apple Maintains Leading Mobile Device Manufacturer...
Milennial Media says Apple continued to be the number one mobile device manufacturer on their platform in Q1, representing 28% of the top manufacturers impression share. Apple iPhone accounted for 15... Read more
Asustek To Launch Three New ZenBook Ultrabook Mode...
Digitimes’ Rebecca Kuo and Steve Shen report that PC-maker Asustek Computer will launch three new models to its ZenBook Prime Ultrabook lineup – the UX21A, UX31A and UX32VD – in June, featuring full... Read more
Yahoo! Introduces Axis Search Browser For Mobile D...
Yahoo! has announced the availability of Yahoo! Axis, a new Web browser tool that it claims will re-imagine how people search and browse on the web, Axis offering a faster, smarter search with... Read more
Android- and iOS-Powered Smartphones Expand Market...
Smartphones powered by Android and iOS mobile operating systems accounted for more than eight out of ten smartphones shipped in the first quarter of 2012 (1Q12), according to the International Data... Read more
Roundup of Memorial Day Weekend MacBook Pro sales,...
 Apple resellers have MacBook Pros on sale for up to $240 off MSRP this Holiday weekend. Here is a roundup of the best prices available from any reseller: (1) B&H Photo has MacBook Pros on sale... Read more
iPad wait times down to 1-3 days at The Apple Stor...
The Apple Store Online is now reporting a 1-3 business day wait on all iPad orders, as it appears that Apple is clearing out their backlog. The iPad is available in Wi-Fi or Wi-Fi + Cellular... Read more
Roundup of Memorial Day Weekend MacBook Air sales,...
 Apple resellers have MacBook Airs on sale for up to $101 off MSRP this Holiday weekend. Here is a roundup of the best prices available from any reseller: (1) B&H Photo has 11-inch and 13-inch... Read more
13″ 2.8GHz MacBook Pro on sale for $100 off MSRP
Adorama has lowered their price on the 13″ 2.8GHz MacBook Pro to $1399 including free shipping plus NY/NJ sales tax only. Their price is $100 off MSRP, and it’s the lowest price for this model from... Read more

Jobs Board

*Apple* Solutions Consultant-Retail Sal...
The Apple Solutions Consultant is an Apple employee who oversees the sales, merchandising, and operations of an Apple Store-in-a-Store in a single unit retail Read more
iPad/iPhone Developer at Recruitarrow (P...
Job Responsibilities and Requirements: These solutions must be aligned with business and IT strategies and comply with the organization's architectural standards. Involved in the full systems life... Read more
Mobile iphone App with API Connections t...
See requirements. Develop mobile app that interfaces to access database on webserver and infusionsoft through API. Desired Skills: iPhone, Mobile, Infusionsoft, API Read more
*Apple* Retail - Manager - Natick Colle...
Much more than just a place for amazing products, the Apple Retail Store serves a dazzling range of needs for its customers. Not only can users get hands-on experience Read more
XML image iPhone App at Elance.com (Uppe...
I want a similar iphone app like the following App below: /us/app/hd-tattoo-designs-catalog/id524766650?mt=8 I want a ... can tell who knows the expertise and who outsources the project to others.... Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.