SwiftUI学习笔记
基本组件的使用
Text
简介
显示文本的组件
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import SwiftUI
struct ContentView: View { var body: some View { Text("文本") .foregroundColor(.blue) .bold() .italic() .font(.system(.largeTitle)) .fontWeight(.medium) .shadow(color: .black, radius: 1, x: 0.0, y: 2)
} }
|
效果图
TextField
简介
文本输入框
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import SwiftUI
struct ContentView: View {
@State var phone: String = "+86182****6667"
var body: some View { VStack { TextField("手机号", text: $phone) { print("changed: \($0)") } onCommit: { print("phone: \(self.phone)") } .foregroundColor(.white) .padding() .background(Color.green) .cornerRadius(10) .padding() } .background(Color.purple) .padding() } }
|
效果
紫色背景是VStack容器
SecureField
简介
功能和使用方法与 TextField差不多, 只是多了个密码显示的功能
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import SwiftUI
struct ContentView: View {
@State var password: String = "admin"
var body: some View { VStack { SecureField("密码", text: $password) { print("commit: \(self.password)") } .foregroundColor(.white) .padding() .background(Color.green) .cornerRadius(10) .padding() } .background(Color.purple) .padding() } }
|
效果
Image
简介
图片显示组件
代码
1 2 3 4 5 6 7 8 9 10
| import SwiftUI
struct ContentView: View {
var body: some View { Image(systemName: "4k.tv") .font(.system(size: 44, weight: .ultraLight)) .border(Color.red, width: 1) } }
|
效果
简介
按钮组件,可以点击触发相关事件
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import SwiftUI
struct ContentView: View {
@State var swither: Bool = false
var body: some View { Button(swither ? "状态A":"状态B") { self.swither.toggle() } .frame(width: 100, height: 44, alignment: .center) .background(swither ? Color.red: Color.blue) .foregroundColor(swither ? .yellow: .white) .cornerRadius(6) } }
|
效果
DatePicker
简介
日期/时间 选择
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import SwiftUI
struct ContentView: View {
@State var date: Date = Date()
var body: some View { DatePicker("日期", selection: $date, displayedComponents: .date) .background(Color.gray) .cornerRadius(4) .padding()
} }
|
效果
Toggle
简介
开关状态组件
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import SwiftUI
struct ContentView: View {
@State var enable: Bool = false
var body: some View { Toggle(enable ? "启用": "禁用", isOn: $enable) .frame(width: 100, height: 22, alignment: .center) .padding() .background(Color.pink) .cornerRadius(6)
} }
|
效果
Slider
简介
滑动条
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import SwiftUI
struct ContentView: View {
@State var power: Float = 0.0
var body: some View { HStack { Slider(value: $power, in: 0...100) .frame(width: 160) Text("能量值:\(power)") .foregroundColor(.blue) .font(.system(size: 12)) .frame(width: 120, height: 44, alignment: .leading) }.padding()
} }
|
效果
Stepper
简介
步进器,-/+控制增减
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import SwiftUI
struct ContentView: View {
@State var count: Int = 0
var body: some View { HStack { Stepper("数量", value: $count, step: 1) { print("changed: \($0)") } Text("\(count)") .frame(width: 60, height: 44, alignment: .center) }.padding()
} }
|
效果