Skip to content
/ SwiftUI Public
forked from Jinxiansen/SwiftUI

`SwiftUI` Framework Usage Guide. ๐Ÿš€

License

Notifications You must be signed in to change notification settings

glaphi/SwiftUI

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

15 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Build Status Swift Xcode Xcode MIT

This article refers to SwiftUI apple example and records the results of the exploration here, I hope to be helpful to you.

For the content described in this article, by default you have some experience based on Swift language development, so it will not describe every detail in detail; if you have doubts about Swift syntax, you can learn Swift Grammar.

When learning and using SwiftUI, if you have any questions, you can join the SwiftUI QQ Group: 816138215 to discuss communication.

ไธญๆ–‡็‰ˆ๐Ÿ‡จ๐Ÿ‡ณ

๐Ÿ’ป Requirements

  • macOS 15 Beta
  • Xcode 11.0 Beta
  • iOS 13.0 Beta

Directory๏ผš

Basic View

Layout

State and Data Flow

Gestures

Basic View

Text

Text is used to display one or more lines of text content with the same effect as UILabel, but it is even better.

If you want to create Text, just create it with Text("SwiftUI"); With chained syntax, you can also add multiple attributes to the text, such as fonts, colors, shadows, spacing between top left and right, and so on.

Example:

Text("SwiftUI")
    .color(.orange)
    .bold()
    .font(.system(.largeTitle))
    .fontWeight(.medium)
    .italic()
    .shadow(color: .black, radius: 1, x: 0, y: 2)
View running results

HStack and VStack controls are used to host multiple views, as mentioned later.

TextField

TextField is used to add a normal input box, which is often used as a text input.

Example๏ผš

TextField(self.$name, placeholder: self.nameText, onEditingChanged: { changed in
    print("onEditing: \(changed)")
}) {
    print("userName: \(self.name)")
    self.endEditing(true)
}}
.padding(10)
.frame(height: 50)
.textFieldStyle(.roundedBorder)
.padding(EdgeInsets(top: 0, leading: 20, bottom: 0, trailing: 20))
View running results

SecureField

SecureField is generally used as a password input. It is used in the same way as TextField. The example and the running effect are the same as TextField.

Image

The Image control is used to display images, example:

Image("icon")
    .resizable()
    .frame(width: Length(100),
           height: Length(100),
           alignment: .center)
View running results

Button

Button is used to respond to click events.

Example:

Button(action: {
    print("Tap")
}) {
   Text("I'm a Button")
}
View running results

PullDownButton

Waiting for release.

ItemBasedPopUpButton

Waiting for release.

NavigationButton

NavigationButtonPage is used to push to the next navigation page.

Example:

NavigationButton(destination: NavigationButtonPage()) {
    Text("NavigationButton").bold().color(.orange).font(.largeTitle)
    }.navigationBarItem(title: Text("Page"))
View running results

PresentationButton

PresentationButton is used to pop up a page.

Example:

PresentationButton(PageRow(title: "PresentationButton", subTitle: "pop up a page"),
                   destination: Text("I'm Text")) {
                    print("Present ๐Ÿฆ„")
                   }
View running results

EditButton

EditButton is used to trigger the editing state, just use the navigationBarItems setting when using it.

Example:

navigationBarItems(trailing: EditButton())
View running results

PasteButton

Waiting for release.

DatePicker

DatePicker is used to select the absolute date of the control.

Example:

DatePicker(
    $server.date,
    minimumDate: Calendar.current.date(byAdding: .year,
                                       value: -1,
                                       to: server.date),
    maximumDate: Calendar.current.date(byAdding: .year,
                                       value: 1,
                                       to: server.date),
    displayedComponents: .date
)
View running results

View running resultsle

View running resultsle is used to switch the selected state, for example:

View running resultsle(isOn: $isOn) {
    Text("State: \(self.isOn == true ? "Open":"Close")")
}.padding(20)
View running results

Slider

Slider A control for selecting values from a finite range of values, example:

Slider(value: $data.rating)
View running results

Stepper

Stepper is used to increase or decrease the value, example:

Stepper(value: $value, step: 2, onEditingChanged: { c in
    print(c)
}) {
    Text("Stepper Value: \(self.value)")
    }.padding(50)
View running results

SegmentedControl

SegmentedControl is used for segmentation condition selection, example:

SegmentedControl(selection: $currentIndex) {
    ForEach(0..<items.count) { index in
        Text(self.items[index]).tag(index)
    }
    }.tapAction {
        print("currentIndex: \(self.currentIndex)")
}
View running results

WebView

WebView is used to display an open web page, example:

struct WebViewPage : UIViewRepresentable {
    func makeUIView(context: Context) -> WKWebView  {
        return WKWebView()
    }
    func updateUIView(_ uiView: WKWebView, context: Context) {
        let req = URLRequest(url: URL(string: "https://www.apple.com")!)
        uiView.load(req)
    }
}
View running results

Alert

Alert is used to display a bullet reminder that needs to be associated with a click event. Example:

presentation($showsAlert, alert: {
                Alert(title: Text("Hello"))
            })
View running results

Layout

HStack

HStack is used to arrange the subviews on a horizontal line.

Example:

HStack {
    Text("made in China.")
    Divider() // Just add a line.
    Text("the People's Republic Of China.")
}
View running results

VStack

VStack is used to arrange the subviews on a vertical line.

Example:

VStack {
    Text("made in China.")
    Divider() // Just add a line.
    Text("the People's Republic Of China.")
}
View running results

ZStack

ZStack is used to override the subview, aligned on two axes.

Example:

ZStack {
    Text("made in China.")
    Divider() // Just add a line.
    Text("the People's Republic Of China.")
}
View running results

List

List list container to display a list of data.

Examples:

List(0..<5) { item in
    Text("Hello World !")
}.navigationBarTitle(Text("List"), displayMode: .large)
View running results

ScrollView

ScrollView is a scroll view container.

Example:

ScrollView {
    Text("SwiftUI").padding(20)
    Divider()
    Image("icon").resizable()
        .frame(width: 300, height: 300, alignment: .center)
    Divider()
    Text("Views and ... user interface.")
    }
    .border(style, width: 1,cornerRadius: 10)
    .padding(10)
    .navigationBarTitle(Text("ScrollView"))
View running results

ForEach

ForEach is used to present a view based on a collection of existing data.

Example:

let data = (0..<5).map { $0 }
var body: some View {
    ForEach(data) { e in
        Text("Hello \(e)")
            .bold()
            .font(.system(size: 25, design: .monospaced))
            .padding(5)
}
View running results

๐Ÿ“Ž About

  • The code involved in the above example is in this repository code. It is recommended to download and run the view.
  • If you have better usage and suggestions about SwiftUI, look forward to sharing it!
  • If there are omissions and errors in the examples in this article, please create a Issue !

โœ‰๏ธ Contacts

email : [email protected]

ๅพฎๅš : @ๆ™‹ๅ…ˆๆฃฎ

๐Ÿ“„ License

SwiftUI is released under the GPL-3.0 license. See LICENSE for details.

About

`SwiftUI` Framework Usage Guide. ๐Ÿš€

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 100.0%