Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [24]

By Root 1004 0
The y value can be a little more problematic. The coordinate system on your Mac’s screen has 0 at the lower-left corner of the screen, with the y value getting higher as you move up toward the top of the screen. The problem here is that not everybody has the same size monitor, so any given y value will be different relative to the top of the screen on different size monitors.

Fortunately, Cocoa will automatically adjust the window’s position so that the window always starts on the screen, even if the location you specify would otherwise place it offscreen somewhere, and because we gave the window an autosave name, every time the user launches the application after the first, the window will be where it was when he last quit the application. But, sometimes it’s important that a window start in a particular position relative to the top of the screen.

Do you see the little diagram at the bottom of the size inspector? It gives you a visual representation of the window’s initial position based on your own screen’s size. The little white box represents the window, and the big box represents the screen minus the menu bar. The little grey triangles on all four sides of the white box let you lock the position of the window relative to the sides of the screen. Clicking a triangle toggles it between being extended, harpoon-like, to the edge of the mini-screen, or retracted to the edge of the window. You can place the window where you want it on your screen, then use those triangles to lock the position relative to the let and top of the screen. Let’s do that now.

You need to set a y value that places the window near the menu bar, but not abutting it. The easiest way to do this is to simply move your application’s main window to where you want it, then press the Use Current button on the size inspector, which will set the initial size and location to the current size and location of the window. You can then tweak the sizes numerically if you need to. Once you have the initial position of the window where you want it on your screen, configure the little “harpoons” so that they are extended on the left and top sides of the white box, and retracted at the right and bottom sides, leaving the inspector looking like Figure 3-9.

Figure 3-9. The gray triangles extend out like little harpoons and anchor the window’s position relative to the side of the screen they extend to.

Designing the Window’s Interface


In the library, expand the Cocoa node if it’s not already expanded, then expand the Views & Cells node and click Buttons. This will present you with a bunch of different buttons (Figure 3-10) that you can use. The first item in the bottom list should now be Push Button, which is a standard OS X button. Grab one of those and drag it over to your window’s interface. Use the blue guidelines to place the button in the lower right part of the window (Figure 3-11).

Figure 3-10. Finding the push button in the library.

Figure 3-11. Placing a push button into our application’s main window, using the blue guidelines to place the button the correct distance from the window’s edges.

Once the button is in the right location, release the mouse, and you’ll have a button on your window. Double-click the button now, which will allow you to edit the button’s title. Change its title from Button to Right.

Drag a second button over from the library and place it on the lower left of the window, again using the blue guidelines. After you place this button, double-click it and change this button’s title to Left.

TIP: Instead of dragging a new button over from the library, you could have held down the option key and dragged the existing button to the left. When you hold down the option key and move an object in Interface Builder, it creates a copy of the object and moves that, leaving the original object untouched.

Bring one more button over, and use the bottom blue guideline to place the button the correct distance from the bottom of the window. Try and place it roughly in the horizontal center of the window, but don’t worry

Return Main Page Previous Page Next Page

®Online Book Reader