ios - Xcode 11 Beta 5 - 模态触发器仅一次
问题描述
我刚刚升级到 Xcode 11 Beta 5 并更新了我的 SwiftUI 项目。
在以前的版本中,我想使用PresentationLink组件来显示模式。我遇到了与现在相同的问题,模态仅显示一次。正如我在其他 SO 帖子中看到的那样,我认为这是一个错误。所以我尝试了升级到 Beta 5 的机会,但仍然没有运气。
我注意到这种行为似乎是由包装在ScrollView组件中引起的。如果我删除 ScrollView 组件,一切都会按预期正常工作。
这是代码:
struct HomeList : View {
var listViewItems = listViewItemsData
@State var show = false
var body: some View {
VStack {
HStack {
VStack(alignment: .leading) {
Text("Project title").font(.largeTitle).fontWeight(.heavy)
Text("Project subtitle").foregroundColor(Color.gray)
}
Spacer()
}.padding(.top, 78).padding(.leading, 60)
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 30) {
ForEach(listViewItems) { item in
GeometryReader { geometry in
Button(action: { self.show.toggle()}) {
ListView(title: item.title, image: item.image, color: item.color, destination: item.destination)
.rotation3DEffect(Angle(degrees: Double((geometry.frame(in: .global).minX - 30) / -30)), axis: (x: 0, y: 10, z: 0))
.sheet(isPresented: self.$show, content: { InformationView() })
}
}.frame(width: 246, height: 360)
}
}.padding(30)
Spacer()
}.frame(width: UIScreen.main.bounds.width, height: 480)
Spacer()
}
}
}
总而言之,如果没有ScrollView包装器,Modal 行为将按预期工作。
我想知道是否有解决方案/解决方法?或者我只需要等待发布:)
从答案编辑:
struct HomeList : View {
var listViewItems = listViewItemsData
@State var show = false
@State var view: AnyView = AnyView(Text(""))
var body: some View {
VStack {
HStack {
VStack(alignment: .leading) {
Text("Project title").font(.largeTitle).fontWeight(.heavy)
Text("Project subtitle").foregroundColor(Color.gray)
}
Spacer()
}.padding(.top, 78).padding(.leading, 60)
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 30) {
ForEach(listViewItems) { item in
GeometryReader { geometry in
Button(action: {
self.show.toggle()
self.view = item.destination
}) {
ListView(title: item.title, image: item.image, color: item.color, destination: item.destination)
.rotation3DEffect(Angle(degrees: Double((geometry.frame(in: .global).minX - 30) / -30)), axis: (x: 0, y: 10, z: 0))
}
}.frame(width: 246, height: 360)
}
}.padding(30)
Spacer()
}.frame(width: UIScreen.main.bounds.width, height: 480)
.sheet(isPresented: self.$show, content: { self.view })
Spacer()
}
}
}
解决方案
这与https://stackoverflow.com/a/57087399/3179416的问题相同
只需将您.sheet
的ForEach
.
import SwiftUI
struct Testing : View {
var listViewItems: [Int] = [1, 2, 3]
@State var show = false
var body: some View {
VStack {
HStack {
VStack(alignment: .leading) {
Text("Project title").font(.largeTitle).fontWeight(.heavy)
Text("Project subtitle").foregroundColor(Color.gray)
}
Spacer()
}.padding(.top, 78).padding(.leading, 60)
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 30) {
ForEach(listViewItems, id: \.self) { item in
GeometryReader { geometry in
Button(action: { self.show.toggle()}) {
Text("Button")
.rotation3DEffect(Angle(degrees: Double((geometry.frame(in: .global).minX - 30) / -30)), axis: (x: 0, y: 10, z: 0))
}
}.frame(width: 246, height: 360)
}
}.padding(30)
Spacer()
}.frame(width: UIScreen.main.bounds.width, height: 480)
.sheet(isPresented: self.$show, content: { Text("Modal") })
Spacer()
}
}
}
推荐阅读
- python - 需要密码时如何在 python 中安全地运行自动 paramiko ssh 任务?
- java - 最后一行的字符串没有给出答案
- heroku - PointDNS - Heroku 应用程序上的“名称由另一个帐户管理”
- android - 错误:电子邮件地址格式错误 - React-native - Firebase
- c++ - 如何将类的非静态方法传递给类的数据成员的构造函数
- php - 配置粘贴在共享主机上的 laravel 应用程序中
- android-studio - 如何在 if 语句中将对象与用户输入进行比较
- excel - 仅使用一个单元格公式计算一行中的重复项(也没有条件格式)
- python - 如何在python中加速numpy数组的乘积
- xcode - 如何使用两个不同的 Info.plist?