iOS swift开发系列--如何给swiftui内容视图添加背景图片显示

我需要在swiftui项目中显示背景图,有两种方式,一种是把图片拖入asset资源中,另外一种是直接把图片放在源码目录下。采用第一种方式,直接把图片拖到资源目录,但是swiftui项目没有弹出, "Copy items if needed"和"Create groups"选项 ,而是copy files to destination,可能会遇到问题No image named 'back' found in asset catalog for。

struct ContentView: View {

var body: some View {

VStack {

// 你的其他控件,例如文本、按钮等

Text("Hello, world!")

.foregroundColor(.white) // 设置文本颜色为白色,以便在背景图片上可见

.font(.largeTitle)

.padding() // 为文本添加内边距

// 你可以在这里添加更多的控件

}

.frame(maxWidth: .infinity, maxHeight: .infinity) // 设置框架以填充其父视图

.background(
Image("back") // 引用你添加的图片
.resizable() // 允许图片被拉伸或压缩
.scaledToFill() // 拉伸图片以填充其容器
.clipped() // 裁剪超出框架的图片部分
.opacity(0.5) // 可选:设置背景图片的透明度
)

.edgesIgnoringSafeArea(.all) // 忽略安全区域,使背景图片铺满整个屏幕

}

}

我们看看第二种

详细内容请看:

iOS swift开发系列--如何给swiftui内容视图添加背景图片显示 -- Idea, Programming ,Share

谢谢支持

相关推荐
HarderCoder3 小时前
iOS 知识积累第一弹:从 struct 到 APP 生命周期的全景复盘
ios
叽哥13 小时前
Flutter Riverpod上手指南
android·flutter·ios
HarderCoder1 天前
Swift 6 并发时代,如何优雅地“抢救”你的单例?
swift
zhangmeng1 天前
FlutterBoost在iOS26真机运行崩溃问题
flutter·app·swift
HarderCoder1 天前
SwiftUI 踩坑记:onAppear / task 不回调?90% 撞上了“空壳视图”!
swift
HarderCoder1 天前
@isolated(any) 深度解析:Swift 并发中的“隔离追踪器”
swift
大熊猫侯佩2 天前
桃花岛 Xcode 构建秘籍:Swift 中的 “Feature Flags” 心法
app·xcode·swift
用户092 天前
SwiftUI Charts 函数绘图完全指南
ios·swiftui·swift
YungFan2 天前
iOS26适配指南之UIColor
ios·swift
HarderCoder2 天前
Swift 6.2 新特性 `@concurrent` 完全导读
swift