Apple 官方教程:Designing a User Interface

Views are the building blocks for constructing your user interface. It’s important to understand how to use views to present your content in a clear, elegant, and useful way. Creating a great user interface that effectively showcases your app’s content is essential to building a successful app. In this chapter, you’ll learn about creating and managing views in a storyboard to define your interface.

The View Hierarchy

Views not only display themselves onscreen and react to user input, they also serve as containers for other views. As a result, views in an app are arranged in a hierarchical structure called the view hierarchy. The view hierarchy defines the layout of views relative to other views. Within that hierarchy, view instances enclosed within a view are called subviews, and the parent view that encloses a view is referred to as its superview. Even though a view instance can have multiple subviews, it can have only one superview.

At the top of the view hierarchy is the window object. Represented by an instance of the UIWindow class, a window serves as the basic container into which you can add your view objects for display onscreen. By itself, a window doesn’t display any content. To display content, you add a content view (with its hierarchy of subviews) to the window.

For a content view and its subviews to be visible to the user, the content view must be inserted into a window’s view hierarchy. When you use a storyboard, this placement is configured automatically for you. The application object loads the storyboard, creates instances of the relevant view controller classes, unarchives the content view hierarchies for each view controller, and then adds the content view of the initial view controller into the window. You’ll learn more about managing view controllers in the next chapter; for now, you’ll focus on creating a hierarchy within a single view controller in your storyboard.

Building an Interface Using Views

When you design your app, it’s important to know what kind of view to use for what purpose. For example, the kind of view you use to gather input text from a user, such as a text field, is different from what you might use to display static text, such as a label. Apps that use UIKit views for drawing are easy to create because you can assemble a basic interface quickly. A UIKit view object is an instance of the UIView class or one of its subclasses. The UIKit framework provides many types of views to help present and organize data.

Although each view has its own specific function, UIKit views can be grouped into seven general categories:

Category

Purpose

Examples

Content

Display a particular type of content, such as an image or text.

Image view, label

Collections

Display collections or groups of views.

Collection view, table view

Controls

Perform actions or display information.

Button, slider, switch

Bars

Navigate, or perform actions.

Toolbar, navigation bar, tab bar

Input

Receive user input text.

Search bar, text view

Containers

Serve as containers for other views.

View, scroll view

Modal

Interrupt the regular flow of the app to allow a user perform some kind of action.

Action sheet, alert view

You can assemble views graphically using Interface Builder. Interface Builder provides a library of the standard views, controls, and other objects that you need to build your interface. After dragging these objects from the library, you drop them onto the canvas and arrange them in any way you want. Next, use inspectors to configure those objects before saving them in a storyboard. You see the results immediately, without the need to write code, build, and run your app.

The UIKit framework provides standard views for presenting many types of content, but you can also define your own custom views by subclassing UIView (or its descendants). A custom view is a subclass of UIView in which you handle all of the drawing and event-handling tasks yourself. You won’t be using custom views in these tutorials, but you can learn more about implementing a custom view in “Defining a Custom View” in View Programming Guide for iOS.

Use Storyboards to Lay Out Views

You use a storyboard to lay out your hierarchy of views in a graphical environment. Storyboards provide a direct, visual way to work with views and build your interface.

As you saw in the first tutorial, storyboards are composed of scenes, and each scene has an associated view hierarchy. You drag a view out of the object library and place it in a storyboard scene to add it automatically to that scene’s view hierarchy. The view’s location within that hierarchy is determined by where you place it. After you add a view to your scene, you can resize, manipulate, configure, and move it on the canvas.

The canvas also shows an outline view of the objects in your interface. The outline view—which appears on the left side of the canvas—lets you see a hierarchical representation of the objects in your storyboard.

The view hierarchy that you create graphically in a storyboard scene is effectively a “shrinkwrapped” set of Objective-C objects. At runtime, these shrinkwrapped objects are unarchived. The result is a hierarchy of instances of the relevant classes configured with the properties you’ve set visually using the various inspectors in the utility area.

Use Inspectors to Configure Views

When working with views in a storyboard, the inspector pane is an essential tool. The inspector pane appears in the utility area above the Object library.

Each of the inspectors provides important configuration options for elements in your interface. When you select an object, such as a view, in your storyboard, you can use each of the inspectors to customize different properties of that object.

  • File. Lets you specify general information about the storyboard.

  • Quick Help. Provides useful documentation about an object.

  • Identity. Lets you specify a custom class for your object and define its accessibility attributes.

  • Attributes. Lets you customize visual attributes of an object.

  • Size. Lets you specify an object’s size and Auto Layout attributes.

  • Connections. Lets you create connections between your interface and source code.

You began working with the Attributes inspector in the first tutorial. You’ll continue using these inspectors throughout the rest of the tutorials to configure views and other objects in your storyboard. In particular, you’ll use the Attributes inspector to configure your views, the Identity inspector to configure your view controllers, and the Connections inspector to create connections between your views and view controllers.

Use Auto Layout to Position Views

When you start positioning views in your storyboard, you need to consider a variety of situations. iOS apps run on a number of different devices, with various screen sizes, orientations, and languages. Instead of designing a static interface, you want it to be dynamic and to seamlessly respond to changes in screen size, device orientation, localization, and metrics.

To help you use views to create a versatile interface, Xcode offers a tool called Auto Layout. Auto Layout is a system for expressing relationships between views in your app’s user interface. Auto Layout lets you define these relationships in terms of constraints on individual views or between sets of views.

The Auto Layout menu, which resides in the bottom-right area of your canvas, has four segments. You use this menu to add various types of constraints to views on your canvas, resolve layout issues, and determine constraint resizing behavior.

You’ll work briefly with Auto Layout in the second tutorial to add support for landscape mode to your ToDoList app.

你可能感兴趣的:(Apple 官方教程:Designing a User Interface)