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

谢谢支持

相关推荐
bybitq3 分钟前
Go函数闭包实战-复用函数
ios·golang·xcode
renke33642 小时前
Flutter 2025 跨平台工程体系:从 iOS/Android 到 Web/Desktop,构建真正“一次编写,全端运行”的产品
android·flutter·ios
Zender Han3 小时前
Flutter 中 AbsorbPointer 与 IgnorePointer 的区别与使用场景详解
android·flutter·ios
Digitally5 小时前
3种简单方法备份 iPhone 短信
ios·iphone
2501_915921435 小时前
Flutter App 到底该怎么测试?如何在 iOS 上进行测试
android·flutter·ios·小程序·uni-app·cocoa·iphone
二流小码农6 小时前
鸿蒙开发:一个底部的曲线导航
android·ios·harmonyos
2501_915909066 小时前
如何在 Windows 上上架 iOS App,分析上架流程哪些是不用mac的
android·macos·ios·小程序·uni-app·iphone·webview
TheNextByte16 小时前
如何用 5 种简单方法抹掉 iPhone 上的密码?
ios·iphone