swiftui - 为什么滚动视图不在顶部?
问题描述
我尝试使用 ZStack 在里面放置背景图像和滚动视图
struct DetailView: View {
var body: some View {
GeometryReader { geometry in
// ZStack: Background, ScrollView(vertical)
ZStack {
Image("background_brown").resizable()
.aspectRatio(geometry.size, contentMode: .fill)
.edgesIgnoringSafeArea(.all)
.frame(width:UIScreen.main.bounds.width, height:UIScreen.main.bounds.height)
.border(Color.yellow, width: borderWidth)
ScrollView(.vertical/, showsIndicators: true, content: {
Text("hello")
}).frame(width:UIScreen.main.bounds.width, height:UIScreen.main.bounds.height)
.border(Color.yellow, width: borderWidth)
}
}
}
}
但是,我发现滚动视图底部超出了屏幕范围。为什么会这样?
解决方案
这是因为您将 ZStack 中的项目设置为具有 UIScreen.main.bounds.height 的框架。因此ZStack
,包括安全区域的 ,从顶部安全区域开始,并将其高度调整为UIScreen.main.bounds.height
。
在您的代码中,您可能只使用geometry.size.height
而不是UIScreen.main.bounds.height
框架,它会起作用。但是,您可以改为使用.background
修饰符,如下所示:
struct DetailView2: View {
var body: some View {
ScrollView(.vertical, showsIndicators: true, content: {
Text("hello")
})
.frame(maxWidth: .infinity)
.background(
GeometryReader { geometry in
Image("background_brown").resizable()
.aspectRatio(contentMode: .fill)
.edgesIgnoringSafeArea(.all)
.frame(width: geometry.size.width, height: geometry.size.height)
}
)
}
}
推荐阅读
- r - 在ggplot2中将表达式更改为粗体,同时保留其大小
- python - 如何在值python之间进行检查
- flutter - 在多个下拉菜单中,如果已在 Flutter 中选中,如何禁用选项?
- php - PHP:获取访问应用程序的时区
- android - 如何使用目标实体更新 Android 上的 Rooms 数据库表中的特定列?
- c# - 将派生类实例列表转换为其基类实例列表
- python - 使用 SQLite 和 SQLAlchemy 查询格式化日期以获取用于绘制图形的月份和年份
- opengl - 一个 VBO 中的多个“精灵”
- python-3.x - 尽管正在执行自定义操作,但 RASA 自定义操作不适用于插槽
- sqlite - 如何正确获取数据库中的下一行