- Is me me or me
- SwiftUI is a framework for ios development using swift language
- Different from Objective-C
- Synchronous learning with Obejctive-C and Android Studio using Java language
layout
First look at an error:
Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type:
The reason is that only one layout can exist in
var body: some View { } , and any more content will report an error; therefore, we can only nest designs in a large layout.
- HStack is placed horizontally, VStack is placed vertically
Add components
- In the past, android can drag and drop components in the xml file to complete the UI design, and use the objective-c ios development to complete the UI design through the storyboard drag and drop components; both can be separated from the graphical interface and the code.
- The current way is to write the code/drag and drop the component to automatically generate the code, and then preview the current layout design on the canvas next to it.
Common components
- Label
Text("手机软件开发") // 是一个label
@State var account: String = "" //变量声明
@State var password: String = ""
- TextField
HStack {
Image(systemName: "person")
TextField("用户名", text: $account, onCommit: {
})
}.padding(.all, 25.0)
- Button and action event writing
Button(action: {
print("login action")
}) {
Text("登陆")
}.padding(.all, 25.0)
interface
The main interface is in the ContentView.swift file.
Corresponding to MainActivity of Android and Main.storyboard of OC-based ios?
(Review: Multiple interfaces
OC-based ios development is to create ViewController,
Android is to create Activity.java, and the activity element must be declared in the AndroidManifest.xml file)
Variables and constants
normal constant
@state variable
Page jump
Review: In the
OC-based ios development, the segue between the control and the ViewController in the storyboard mode is directly completed in the
Android Studio. In the android development: use intent to jump
Use NavigationView in SwiftUI
NavigationView {
NavigationLink(
destination: LoginView(),isActive: $loginAccountIsActive){
Text("点我跳转")
}
}
Realize the effect: