Читать книгу Swift iOS 24-Hour Trainer - Mishra Abhishek - Страница 4

Section I
Hello iOS!
Lesson 1
Hello iOS!
TRY IT

Оглавление

In this Try It, 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 New 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 Navigators 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 Utilities 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 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 at www.wrox.com/go/swiftios.

• iPhone App 2x: Use the file iPhoneAppIcon2x.png.

• iPhone App 3x: Use the file iPhoneAppIcon3x.png.

• iPad App 1x: Use the file iPadAppIcon1x.png.

• iPad App 2x: Use the file iPadAppIcon2x.png.

After these assignments are made, your scene should resemble Figure 1.12.


Figure 1.12


• Test your app in the iOS Simulator by clicking the Run button in the Xcode toolbar. Alternatively, you can use the Project Run menu item.

REFERENCE

To see some of the examples from this lesson, watch the Lesson 1 video online at www.wrox.com/go/swiftiosvid.

Swift iOS 24-Hour Trainer

Подняться наверх