当前位置: 首页 > news >正文

SwiftUI-基础

应用入口

Main函数与App结构体的绑定,遵循App协议

@main
struct BaseApp: App {var body: some Scene {WindowGroup {ContentView()}}
}

兼容UIApplicationDelegate

@main
struct BasicApp: App {@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegatevar body: some Scene { ... }
}class AppDelegate : NSObject, UIApplicationDelegate {func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {// launching...return true}
}

Swift 结构体与类的构造函数

// struct
struct BasicApp: App {init() { ... }
}// class
class AppDelegate: NSObject, UIApplicationDelegate {override init () { ... }
}

视图预览

遵守View协议,定义View结构体

struct ContentView: View {var body: some View {Text("Hello, world!").padding()}
}

遵守PreviewProvider协议,定义Previews结构体

struct ContentView_Previews: PreviewProvider {static var previews: some View {ContentView()}
}

UI布局

struct ContentView: View {var body: some View {VStack(alignment: .center, spacing: 30) {Text("文本").foregroundColor(.black)Button {print("target_action")} label: {Text("按钮").foregroundColor(.blue)}}}
}

VStack - 纵向布局

var body: some View {VStack(alignment: .leading, spacing: 10) { // 左对齐、元素间隔10pxText("Column 1")Text("Column 2")}.padding(20) // 内边距20px
}

HStack - 横向布局 

var body: some View {HStack(spacing: 10) { // 居中、元素间隔10pxText("Column 1")Text("Column 2")}.padding() // 内边距-默认值
}

Text - 文本

var body: some View {Text("Hello, SwiftUI!").font(.title) // 字体设置.foregroundColor(.blue) // 文本颜色设置.multilineTextAlignment(.center) // 多行文本对齐方式设置.lineLimit(1) // 文本行数限制设置
}

Button - 按钮

var body: some View {Button { print("Button tapped") // 按钮点击事件处理} label: {Text("Click Me") // 按钮显示的文本.font(.title) // 字体设置.padding() // 内边距设置.background(Color.blue) // 背景颜色设置.foregroundColor(.white) // 前景颜色设置.cornerRadius(10) // 圆角设置}
}

Image - 图像

var body: some View {Image("exampleImage") // 根据名称加载图像.resizable() // 图像可调整大小.frame(width: 100, height: 100) // 设置图像框的尺寸.background(.gray)
}

 TextField - 文本输入

@State private var textInput: String = ""var body: some View {VStack {TextField("Enter text", text: $textInput).textFieldStyle(RoundedBorderTextFieldStyle()) // 输入框样式设置.padding() // 内边距设置Text("You entered: \(textInput)")}.padding()
}

ScrollView - 滚动视图 

var body: some View {ScrollView {ForEach(1...10, id: \.self) { index inText("Item \(index)").padding()}}
}

List - 表视图

var body: some View {List {Text("Row 1")Text("Row 2")Text("Row 3")}.listStyle(InsetListStyle()) // 样式设置
}

TextView - 多行文本输入

@State private var textInput: String = ""var body: some View {VStack {TextEditor(text: $textInput) // 多行文本输入视图.frame(height: 200) // 设置高度.border(Color.gray, width: 1) // 边框设置Text("You entered: \(textInput)")}.padding()
}

Alert - 警告弹窗

@State private var showAlert = falsevar body: some View {Button {showAlert = true} label: {Text("Show Alert")}.alert(isPresented: $showAlert) {Alert(title: Text("Warning"), message: Text("This is an alert!"), dismissButton: .default(Text("OK")))}
}

ActionSheet - 底部弹窗

@State private var showActionSheet = falsevar body: some View {Button {showActionSheet = true} label: {Text("Show ActionSheet")}.actionSheet(isPresented: $showActionSheet) {ActionSheet(title: Text("Options"), message: Text("Choose an action"), buttons: [.default(Text("Option 1"), action: {// Option 1 action}),.default(Text("Option 2"), action: {// Option 2 action}),.cancel() // 取消按钮])}
}

Picker - 选择器

@State private var selectedOption = 0
let options = ["Option 1", "Option 2", "Option 3"]var body: some View {VStack {Text("Selected option: \(options[selectedOption])")Picker("Options", selection: $selectedOption) {ForEach(0..<options.count, id: \.self) { index inText(options[index])}}.pickerStyle(WheelPickerStyle()) // 选择器样式设置}.padding()
}
http://www.lryc.cn/news/99164.html

相关文章:

  • vue。cli怎么使用自定义组件,会有哪些问题
  • linux----vim的使用
  • 95. Python基础教程:异常处理try...except语句
  • 详解rocketMq通信模块升级构想
  • 【BOOST程序库】对字符串的处理
  • (学习笔记-内存管理)虚拟内存
  • JVM理论(七)性能监控与调优
  • 复现YOLOv8改进最新MPDIoU:有效和准确的边界盒回归的损失,打败G/E/CIoU,效果明显!!!
  • LT6911C 是一款HDMI 1.4到双端口MIPIDSI/CSI或者LVDS加音频的一款高性能芯片
  • vue动态引入静态资源
  • perl 强制覆盖拷贝文件
  • C语言每日一题之整数求二进制1的个数
  • AcWing 4443.无限区域
  • 2D坐标系下的点的转换矩阵(平移、缩放、旋转、错切)
  • 【Rabbitmq】报错:ERROR CachingConnectionFactory Channel shutdown: channel error;
  • el-table组件的el-table-column电脑端使用fixed属性固定,移动端不使用固定,怎么实现?
  • RocketMQ 行业分享
  • 物联网场景中的边缘计算解决方案有哪些?
  • 【C++ 进阶】学习导论:C/C++ 进阶学习路线、大纲与目标
  • 【数据结构】实验七:字符串
  • 排序算法、
  • rbd快照管理、rbd快照克隆原理与实现、rbd镜像开机自动挂载、ceph文件系统、对象存储、配置对象存储客户端、访问Dashboard
  • vue、vuex、vue-router初学导航配合elementui及vscode快捷键
  • Elasticsearch:使用 ELSER 释放语义搜索的力量:Elastic Learned Sparse EncoderR
  • MySQL数据库分库分表备份(shell脚本)
  • 建造者设计模式go实现尝试
  • 创建交互式用户体验:探索JavaScript中的Prompt功能
  • 自然语言处理从入门到应用——LangChain:提示(Prompts)-[提示模板:基础知识]
  • OpenPCDet调试出现的问题
  • 【业务功能篇58】Springboot + Spring Security 权限管理 【下篇】