Скачать книгу

you build a simple iPhone application using Xcode 7 that displays the text “Hello Swift” in the center of the screen. You will also provide application icons and a launch file.

      Lesson Requirements

      • Launch Xcode.

      • Create a new project based on the Single View Application template.

      • Edit a storyboard in Interface Builder.

      • Display the Xcode Utilities area.

      • Set up an application icon.

      • Set up a launch file.

      • Test an app in the iOS Simulator.

      REFERENCE

      The code for this Try It is available at www.wrox.com/go/swiftios.

      Hints

      Download and install the latest version of Xcode and the iOS SDK on your Mac; then launch Xcode.

      Step-by-Step

      • Create a Single View Application in Xcode called HelloSwift.

      1. Launch Xcode.

      2. To create a new project, select the File arrow New arrow Project menu item.

3. Choose the Single View Application (see Figure 1.5) template for iOS and click Next.

Figure 1.5

4. Use the following information in the project options dialog box (see Figure 1.6) and click Next.

      • Product Name: HelloSwift

      • Organization Name: Your company

      • Organization Identifier: com.wileybook

      • Language: Swift

      • Devices: Universal

      • Use Core Data: Unchecked

      • Include Unit Tests: Unchecked

      • Include UI Tests: Unchecked

Figure 1.6

      5. Select a folder where this project should be created.

      6. Ensure the Source Control checkbox is not selected.

      7. Click Create.

• Edit the Main.storyboard file in Interface Builder (see Figure 1.7).

Figure 1.7

      1. Ensure the project navigator is visible and the HelloSwift project is selected and expanded. To show the project navigator, use the View arrow Navigators arrow Show Project Navigator menu item. To expand a project, click the triangle next to the project name in the project navigator.

      2. Click the Main.storyboard file to select it.

      3. Ensure the Attribute inspector is visible by selecting the View arrow Utilities arrow Show Utilities menu item.

      4. Click the white background area of the default scene in the storyboard.

5. Under the View section of the Attribute inspector, click once on the Background item to change the background color. This is shown in Figure 1.8. Pick any color you want.

Figure 1.8

6. From the Object library in the bottom-right corner, select Label and drop it onto the View (see Figure 1.9). You can use the search box to narrow your choices.

Figure 1.9

      7. Change the text displayed in the Label to “Hello Swift” by editing the value of the Text attribute in the Attribute inspector.

      8. Position the label anywhere within the scene using the mouse.

      • Create layout constraints.

      1. Select the label in the storyboard scene by clicking on the label once. Change the size of the label so that the label is large enough to show the text “Hello Swift” fully. To do this use the Editor arrow Size To Fit Content menu item.

2. Select the label in the storyboard and bring up the Align constraints popup window by clicking the Align button at the bottom right corner of the storyboard (see Figure 1.10).

Figure 1.10

In this popup window, setup the following options (see Figure 1.11):

      • Horizontally in Container: Checked

      • Vertically in Container: Checked

      • Update Frames: All Frames In Container

Figure 1.11

      Click the Add 2 constraints button in the popup to apply these layout constraints to the label and dismiss the popup.

      NOTE

      Selecting All Frames in Container in the Update Frames combo box will force the scene to update the position of the label using the constraints you have just specified.

      • Set up a launch file.

      1. Select the LaunchScreen.Storyboard file in the project navigator.

      2. Use the Attribute Inspector to change the background color of the launch file to a different color than that of the scene in the main storyboard.

      • Set up an application icon.

      1. Select the Assets.xcassets item in the project navigator to open the asset bundle. Select the AppIcon asset within this bundle.

      2. Use drag-and-drop to assign images to the iPhone App and iPad App placeholders. You can obtain the images from the resources available for this lesson on the book's website

Скачать книгу