Please complete Chinese tutorial and see github.com/WillieWangW...
Creating and combinations View
This section will guide you to build a place to discover and share your favorite App in iOS -
Landmarks
. First, we show the landmark building view details.
Landmarks
Usestacks
willimage
,text
like components are combined and layered, in order to give the layout view. If you want to add to the map view, we need to introduce standardMapKit
components. When we adjust the design, Xcode can make real-time feedback, so that we can see how these adjustments are converted to code.Download the project file and follow the steps below.
- Estimated Completion Time: 40 minutes
- The initial project files: Download
1. Create a new project and view Canvas
Used SwiftUI
to create a new Xcode project template app, and look at the canvas.
1.1 Open Xcode, click on Xcode startup window Create a new Xcode project
, or select File
> New
> Project
.
1.2 Select the iOS
platform, Single View App
template, and then click Next
.
1.3 input Landmarks
as Product Name
, check Use SwiftUI
box, and then click Next
. Select a location to save the project.
1.4 Project navigator
, check ContentView.swift
.
By default, SwiftUI
View document declares two structures. The first structure follow a View
protocol describing the content and layout view. The second view of the structure declaration preview.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello World")
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
复制代码
1.5 canvas
, click Resume
to display a preview.
Tip: If you do not
canvas
, selectEditor
>Editor and Canvas
to display.
1.6 body
Properties, Hello World
change their greetings. When you change the code, the preview will be updated in real time.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello SwiftUI!")
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
复制代码
2. Custom Text View
In order to customize the view of the display, we can change your code, or use inspector
to help us write code.
In the build Landmarks
process, we can use any editor to work: write the source code, modify canvas
, or through inspectors
, no matter what tools to use, the code will be kept up to date.
Next, we use inspector
to customize text view
.
2.1 In the preview, hold down Command
and click the greeting to display the editing window, and then select Inspect
.
Editing window displays the different attributes can be modified, depending on which type of view.
2.2 with inspector
the text changed Turtle Rock
, this is the first landmark name displayed in the app.
2.3 will be Font
amended to Title
.
This change makes the text using the system font, after which it will be able to display the user's preferences and font size settings are correct.
Edit the code by hand to add the .color(.green) modifier; this changes the text’s color to green.
To customize a SwiftUI view, you call methods called modifiers. Modifiers wrap a view to change its display or other properties. Each modifier returns a new view, so it’s common to chain multiple modifiers, stacked vertically.
2.4 in the code to add .color(.green)
, change text color is green.
If you want to customize the SwiftUI
view, we can call a class called modifiers
methods. Such methods to change the display attributes or other packaging a view. Each modifiers
method returns a new view, a chain so we can call multiple modifiers
methods.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Turtle Rock")
.font(.title)
.color(.green)
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
复制代码
view the true source of the code is in fact, when we used inspector
to modify or delete modifiers
the time, Xcode will update our code immediately.
2.5 This time we hold down in the code editor area Command
, click Text
the statement to open inspector
, and then select Inspect
. Click the Color menu and select Inherited
, so the text has changed back to black.
Note 2.6, Xcode will automatically update the code for modifications, such as deleting .color(.green)
.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Turtle Rock")
.font(.title)
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
复制代码
3. View combination with Stacks
After create a title on a view, let's add text view, which is used to display the subject of detailed information, such as the name and the state where the park.
Creating SwiftUI
time view, we can view a body
description of its contents, layout and behavioral attributes. Since the body
property returns only a single View, we can use Stacks
to combine a plurality of View and embedding, so that they are horizontal, vertical or in order from back to front together.
In this section, we use levels stack
to display the details of the park, and then perpendicular stack
to the above details on the title.
We can use Xcode editing features will be embedded view to a container, you can use inspector
or help
find more help.
3.1 Hold down Command
and click the text initialization method of view, select the edit window Embed in VStack
.
Next, from the Library
drag of a Text view
added to the stack
medium.
3.2 Click the plus button in the upper right corner of de Xco (+)
open Library
, then a drag Text view
, put the code Turtle Rock
back.
3.3 will Placeholder
change Joshua Tree National Park
.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("Turtle Rock")
.font(.title)
Text("Joshua Tree National Park")
}
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
复制代码
Place adjust the layout view to meet the demand.
3.4 The location of view font
set .subheadline
.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("Turtle Rock")
.font(.title)
Text("Joshua Tree National Park")
.font(.subheadline)
}
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
复制代码
3.5 editing VStack
initialization method, in the view leading
alignment.
By default, stacks
it will content centered along its axis, and provided a pitch appropriate to the context.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack(alignment: .leading) {
Text("Turtle Rock")
.font(.title)
Text("Joshua Tree National Park")
.font(.subheadline)3
}
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
复制代码
Next, we add another location to the right of the text view to display the state park is located.
3.6 In canvas
the hold down Command
, click Joshua Tree National Park
, then select Embed in HStack
.
After 3.7 Location Added a text view, will be Placeholder
modified California
, and then font
arranged .subheadline
.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack(alignment: .leading) {
Text("Turtle Rock")
.font(.title)
HStack {
Text("Joshua Tree National Park")
.font(.subheadline)
Text("California")
.font(.subheadline)
}
}
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
复制代码
3.8 in the horizontal stack
add one Spacer
to split and fixed Joshua Tree National Park
and California
so that they will share the entire width of the screen.
spacer
It contains the deployable view, so that they all share the parent view of the space, not only its size is defined by its content.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack(alignment: .leading) {
Text("Turtle Rock")
.font(.title)
HStack {
Text("Joshua Tree National Park")
.font(.subheadline)
Spacer()
Text("California")
.font(.subheadline)
}
}
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
复制代码
3.9 Finally, use .padding()
this method to modify the landmark name and leave some space information.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack(alignment: .leading) {
Text("Turtle Rock")
.font(.title)
HStack {
Text("Joshua Tree National Park")
.font(.subheadline)
Spacer()
Text("California")
.font(.subheadline)
}
}
.padding()
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
复制代码
4. Custom Image View
After get the name and location of view, we have to add a picture to the landmark.
There is no need to add a lot of code, just create a custom view, and then add to the picture masking, borders, and shading can be.
First, add pictures to the project asset catalog
in.
4.1 project Resources
to find the folder turtlerock.png
and drag it to asset catalog
the editor. Xcode will create a picture image set
.
Next, create a new SwiftUI
view to customize the image view.
4.2 Select File
> New
> File
Open the Template Chooser. In User Interface
, select SwiftUI View
, then click Next
. Name the file CircleImage.swift
, and then click Create
.
Now preparatory work has been completed.
4.3 Image(_:)
initialization method will be replaced with a text view Turtle Rock
pictures.
CircleImage.swift
import SwiftUI
struct CircleImage: View {
var body: some View {
Image("turtlerock")
}
}
struct CircleImage_Preview: PreviewProvider {
static var previews: some View {
CircleImage()
}
}
复制代码
4.4 calls .clipShape(Circle())
trim an image into a circle.
Circle
It can be used as a mask's shape, can also stroke
or fill
form view.
CircleImage.swift
import SwiftUI
struct CircleImage: View {
var body: some View {
Image("turtlerock")
.clipShape(Circle())
}
}
struct CircleImage_Preview: PreviewProvider {
static var previews: some View {
CircleImage()
}
}
复制代码
4.5 Create another gray stroke
's circle
, then as overlay
added to the picture, forming a picture frame.
CircleImage.swift
import SwiftUI
struct CircleImage: View {
var body: some View {
Image("turtlerock")
.clipShape(Circle())
.overlay(
Circle().stroke(Color.gray, lineWidth: 4))
}
}
struct CircleImage_Preview: PreviewProvider {
static var previews: some View {
CircleImage()
}
}
复制代码
4.6 then the next to add a shadow radius of 10 point.
CircleImage.swift
import SwiftUI
struct CircleImage: View {
var body: some View {
Image("turtlerock")
.clipShape(Circle())
.overlay(
Circle().stroke(Color.gray, lineWidth: 4))
.shadow(radius: 10)
}
}
struct CircleImage_Preview: PreviewProvider {
static var previews: some View {
CircleImage()
}
}
复制代码
4.7 The color of the border changed to white
complete the image view.
CircleImage.swift
import SwiftUI
struct CircleImage: View {
var body: some View {
Image("turtlerock")
.clipShape(Circle())
.overlay(
Circle().stroke(Color.white, lineWidth: 4))
.shadow(radius: 10)
}
}
struct CircleImage_Preview: PreviewProvider {
static var previews: some View {
CircleImage()
}
}
复制代码
The use UIKit and SwiftUI
At this point, we are ready to create a map view, then use MapKit
the MKMapView
class to render a map.
In SwiftUI
use, UIView
a subclass, you need to follow in other view packaging UIViewRepresentable
protocol SwiftUI
view in. SwiftUI
And it includes WatchKit
, AppKit
View similar agreements.
First, we can create a presentation MKMapView
to customize the view.
5.1 Select File
> New
> File
, select iOS
the platform, select the SwiftUI View
template, and then click Next
. The new file name MapView.swift
, and then click Create
.
5.2 to MapKit
add a import
statement to declare MapView
type follows UIViewRepresentable
.
Xcode can ignore the error, the next steps will be to solve these problems.
MapView.swift
import SwiftUI
import MapKit
struct MapView: UIViewRepresentable {
var body: some View {
Text("Hello World")
}
}
struct MapView_Preview: PreviewProvider {
static var previews: some View {
MapView()
}
}
复制代码
UIViewRepresentable
Protocol need to implement two methods: makeUIView(context:)
to create a MKMapView
, updateUIView(_:context:)
to view and respond to configuration changes.
5.3 with makeUIView(context:)
alternative method body
attribute, the method creates and returns a null MKMapView
.
MapView.swift
import SwiftUI
import MapKit
struct MapView: UIViewRepresentable {
typealias UIViewType = MKMapView
func makeUIView(context: UIViewRepresentableContext<MapView>) -> MKMapView {
return MKMapView(frame: .zero)
}
}
struct MapView_Preview: PreviewProvider {
static var previews: some View {
MapView()
}
}
复制代码
5.4 to achieve updateUIView(_:context:)
a method, to set the coordinate map view, so that in Turtle Rock
the center.
MapView.swift
import SwiftUI
import MapKit
struct MapView : UIViewRepresentable {
typealias UIViewType = MKMapView
func makeUIView(context: UIViewRepresentableContext<MapView>) -> MKMapView {
return MKMapView(frame: .zero)
}
func updateUIView(_ uiView: MKMapView, context: UIViewRepresentableContext<MapView>) {
let coordinate = CLLocationCoordinate2D(
latitude: 34.011286, longitude: -116.166868)
let span = MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0)
let region = MKCoordinateRegion(center: coordinate, span: span)
uiView.setRegion(region, animated: true)
}
}
struct MapView_Preview: PreviewProvider {
static var previews: some View {
MapView()
}
}
复制代码
When the preview in static mode
the display only SwiftUI
view. Because MKMapView
a UIView
subclass, you need to switch to the real-time mode to see the map.
5.5 click Live Preview
can switch to Live Preview mode, sometimes used Try Again
or Resume
buttons.
After a few moments, you will see Joshua Tree National Park
the map, which is the Turtle Rock
hometown.
6. Develop View details
Now that we have completed all the required components: name, location, pictures and maps round.
Continue to use the current tools, these components combine to become in line with final design details view.
6.1 In the project navigation, select the ContentView.swift
file.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack(alignment: .leading) {
Text("Turtle Rock")
.font(.title)
HStack {
Text("Joshua Tree National Park")
.font(.subheadline)
Spacer()
Text("California")
.font(.subheadline)
}
}
.padding()
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
复制代码
Of 6.2 prior to VStack
embed another new VStack
medium.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
VStack(alignment: .leading) {
Text("Turtle Rock")
.font(.title)
HStack(alignment: .top) {
Text("Joshua Tree National Park")
.font(.subheadline)
Spacer()
Text("California")
.font(.subheadline)
}
}
.padding()
}
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
复制代码
6.3 custom MapView
added to the stack
top, using the frame(width:height:)
method to set the MapView
size.
If you specify only height
parameters, view automatically adjust the width of its contents. This section, MapView
expands to fill all available space.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
MapView()
.frame(height: 300)
VStack(alignment: .leading) {
Text("Turtle Rock")
.font(.title)
HStack(alignment: .top) {
Text("Joshua Tree National Park")
.font(.subheadline)
Spacer()
Text("California")
.font(.subheadline)
}
}
.padding()
}
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
复制代码
6.4 Click the Live Preview
button to see the rendering in combination view the map.
In this process, we can continue to edit view.
6.5 will be CircleImage
added to the stack
medium.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
MapView()
.frame(height: 300)
CircleImage()
VStack(alignment: .leading) {
Text("Turtle Rock")
.font(.title)
HStack(alignment: .top) {
Text("Joshua Tree National Park")
.font(.subheadline)
Spacer()
Text("California")
.font(.subheadline)
}
}
.padding()
}
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
复制代码
6.6 In order to image view
layout map view
the top, we need to set the picture -130 points
offset, and fill from the bottom -130 points
.
After the picture moves up to make room for the text.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
MapView()
.frame(height: 300)
CircleImage()
.offset(y: -130)
.padding(.bottom, -130)
VStack(alignment: .leading) {
Text("Turtle Rock")
.font(.title)
HStack(alignment: .top) {
Text("Joshua Tree National Park")
.font(.subheadline)
Spacer()
Text("California")
.font(.subheadline)
}
}
.padding()
}
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
复制代码
6.7 externally VStack
adding a base spacer
, to push content to the top of the screen.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
MapView()
.frame(height: 300)
CircleImage()
.offset(y: -130)
.padding(.bottom, -130)
VStack(alignment: .leading) {
Text("Turtle Rock")
.font(.title)
HStack(alignment: .top) {
Text("Joshua Tree National Park")
.font(.subheadline)
Spacer()
Text("California")
.font(.subheadline)
}
}
.padding()
Spacer()
}
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
复制代码
6.8 Finally, in order to expand map content to the edge of the screen, you need to edgesIgnoringSafeArea(.top)
add to the map view.
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
MapView()
.edgesIgnoringSafeArea(.top)
.frame(height: 300)
CircleImage()
.offset(y: -130)
.padding(.bottom, -130)
VStack(alignment: .leading) {
Text("Turtle Rock")
.font(.title)
HStack(alignment: .top) {
Text("Joshua Tree National Park")
.font(.subheadline)
Spacer()
Text("California")
.font(.subheadline)
}
}
.padding()
Spacer()
}
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
复制代码
Reproduced in: https: //juejin.im/post/5cfa706f6fb9a07ee1691a46