[Xcode 12.3] Introduction to Apple Platform App Development-Object Edition- [Swift UI]

What is SwiftUI

SwiftUI is a UI framework that builds the application UI of the Apple platform with Declarative Syntax with the development language Swift.

Like SwiftUI, Flutter is a well-known framework for building application UIs for Apple products, and the differences are summarized in the table below.

SwiftUI Flutter
Supported platforms Apple only iOS, Android, Web
IDE Xcode Android Studio, VS code
Description format Declarative syntax Declarative syntax
development language Swift Dart, Widget
Code amount Few Many
Scalability Inferior to Flutter high

When developing apps for the Apple platform, we will focus on the official Apple framework SwiftUI.

SwiftUI

text

Text

スクリーンショット 2021-01-13 15.12.32.png

text


Text("<String>")

Link

スクリーンショット 2021-01-09 13.25.55.png

Link


Link(destination: <URL>) { Text("<label>") }

Text Field & Secure Field

スクリーンショット 2021-01-13 12.28.32.png ↑ No input (placeholder text is displayed)

スクリーンショット 2021-01-13 15.39.24.png

↑ Text field. State during input

スクリーンショット 2021-01-13 12.14.25.png ↑ Secure text field. State during input

Text fields and secure text fields


@State private var value: String = ""

//Linking is possible by setting the values ​​of TextField and SecureField to the same property.
TextField("<Placeholder text>", text: $value)
SecureField("<Placeholder text>", text: $value)

Text editor

スクリーンショット 2021-01-13 15.33.58.png

Text editor


@State private var value: String = ""

TextEditor(text: $value)  //Text editors do not implement placeholder text by default

button

System Button

スクリーンショット 2021-01-08 19.17.40.png

button


Button(action: <Execution method>) { <label> }

Editor button

スクリーンショット 2021-01-08 19.51.43.png ↑ Editor is inactive

スクリーンショット 2021-01-08 19.53.10.png ↑ The editor is active

Editor button


EditButton()

Switch (Toggle Button)

スクリーンショット 2021-01-13 15.42.33.png

switch


@State private var flg: Bool = True

Toggle(isOn: $flg) {
    <label>
}

Sign In With Apple Button

スクリーンショット 2021-01-13 14.22.30.png

Sign-in button


import Authentication Services  //Packages required to implement ASAuthorizationAppleIDButton

struct SignInWithAppleButton: UIViewRepresentable {
    func makeUIView(context: Context) -> ASAuthorizationAppleIDButton {
    //ASAuthorizationAppleIDButton is Apple's standard button component,
    //It is not provided as a standard component of SwiftUI, so it must be implemented as a View component of SwiftUI.
        let button = ASAuthorizationAppleIDButton()
        return button
    }

    func updateUIView(_: ASAuthorizationAppleIDButton, context _: Context) [}
}

SignInWithAppleButton()

If you want to implement authentication function, [here] See (https://inon29.hateblo.jp/entry/2020/03/07/171915).

Picker

Picker

スクリーンショット 2021-01-13 11.30.57.png

Picker-


Picker(selection: .<Selected value property name>, label: Text(<label>)) {
    Text(<label>).tag(<Selection value>)
}

Color Picker

スクリーンショット 2021-01-08 19.32.30.png

Color picker


ColorPicker("<label>", selection: .constant(.<Default color>))

Date Picker

スクリーンショット 2021-01-08 19.37.47.png

Date picker


DatePicker(selection: .constant(Date()), label: { Text("<label>") })

list

List

スクリーンショット 2021-01-09 13.34.39.png

list


List { <content> }

Label

スクリーンショット 2021-01-09 13.22.14.png

label


Label("<label>", systemImage: "<symbol>")

Section

スクリーンショット 2021-01-13 12.11.59.png

section


Section(header: Text(<label>)) {
    <content>
}

Disclosure Group

スクリーンショット 2021-01-08 19.43.42.png ↑ Closed state

スクリーンショット 2021-01-08 19.44.11.png ↑ Open state

Outline (Outline Group)

スクリーンショット 2021-01-09 17.01.28.png ↑ No List Ver.

スクリーンショット 2021-01-09 17.02.36.png ↑ With List Ver.

outline(Since the implementation was difficult, I wrote the source code)


struct CityData: Identifiable {  //The data used in the Outline Group must conform to the Identifiable protocol
    //Structure"CityData"Property is id, name,3 sites
    let id = UUID()  //UUID(Unique ID)… 128-bit hexadecimal number
    let name: String
    var sites: [CityData]?  //Type method result()Will be assigned later by

    static func result() -> [CityData] {  //Type method(Static method)Definition adds static keyword
        let city1 = [CityData(name: "Site A"), CityData(name: "Site B")]
        let city2 = [CityData(name: "Site C"), CityData(name: "Site D")]

        return [CityData(name: "city1", sites: city1),
                CityData(name: "city1", sites: city1)]
    }  //Depending on the type method, the values ​​of sites will be as follows
       // [name: city1, sites: Optional([name: site A, sites: nil], [name: Site B, sites: nil]) 
       //  name: city2, sites: Optional([name: site C, sites: nil], [name: Site D, sites: nil])]
}

struct ContentView: View {
    var body: some View {
        List {
            ForEach(CityData.result()) { city in  // CityData.sites name: city1,Execute the following processing for each of city2
                OutlineGroup(city, children: \.sites) { site in  // CityData.sites.sites name: site A,Execute the following processing for each of site B
                    Text(site.name)
                }
            }
        }
    }
}

Identifiable protocol


public protocol Identifiable {
    associatedtype ID: Hashable  //Type parameters(=Placeholder)"ID"Is"Hashable"Conforms to protocol
    var id: Self.ID { get }  //Property"id"Is read-only and the instance's own type parameter"ID"Fits
}

Hashable protocol


public protocol Hashable{
    func hash(into hasher: inout Hasher)
    var hashValue: Int { get }  

Group Box

スクリーンショット 2021-01-09 13.19.20.png

Group box


GroupBox(label: <label>) { <content> }

group


DisclosureGroup("<label>") { <content> }

Form

スクリーンショット 2021-01-09 13.14.02.png

Form


Form { <content> }

View

Navigation View, Navigation Link

Simulator Screen Shot - iPhone 12 - 2021-01-09 at 13.57.48.png ↑ Before screen transition

Simulator Screen Shot - iPhone 12 - 2021-01-09 at 13.57.51.png ↑ After screen transition

Navigation link


NavigationView {  //NavigationLink is described within the scope of NavigationView
    NavigationLink(destination: <View protocol compliant instance>) { <label> }
}

Progress View

スクリーンショット 2021-01-13 11.41.56.png

Progress view


ProgressView(value: <0~Progress value of 1>)

Scroll View

スクリーンショット 2021-01-13 11.58.12.png

Scroll view


ScrollView(.<direction>) {
    VStack {   //In the case of vertical direction, put together with VStack
        <content>
    }
    .frame(maxWidth: .infinity)  //Maximize coverage
}

Slider & Stepper

スクリーンショット 2021-01-13 14.36.19.png ↑ Slider

スクリーンショット 2021-01-13 14.38.12.png ↑ Stepper

Slider and stepper


@State private var value: Double = 0

//Linking is possible by setting the values ​​of Slider and Stepper to the same property.
Slider(value: $value, in: 0...100)
Stepper(value: $value, in: 0...100) {
    <label>
}

Tab View

スクリーンショット 2021-01-13 14.46.24.png

Tab bar


@State private var selection = 0 //Properties that hold selection tabs

TabView(selection: $selection) {
    Text("<content>").tabItem { Text(<label>) }.tag(0)
    Text("<content>").tabItem { Text(<label>) }.tag(1)
}

Recommended Posts

[Xcode 12.3] Introduction to Apple Platform App Development-Object Edition- [Swift UI]
XVim2 introduction memo to Xcode12.3
Introduction to swift practice output Chapter5
Introduction to swift practice output Chapter 5 Part 2
Introduction to Ratpack (Extra Edition) --Using Sentry
Shiritori map app made with Swift UI
Try to access the on-premise system from SAP Cloud Platform --Java App Edition