【发布时间】:2019-07-30 19:39:44
【问题描述】:
我刚刚升级到 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()
}
}
}
【问题讨论】: