Xcode 11 Beta 5-模式仅触发一次 [英] Xcode 11 Beta 5 - Modal triggers only once
问题描述
我刚刚升级到Xcode 11 Beta 5并更新了我的SwiftUI项目.
在以前的版本中,我想使用 PresentationLink 组件显示模式.我现在遇到了同样的问题,模态只显示了一次.正如我在其他SO帖子中看到的那样,我认为这是一个错误.因此,我尝试了升级到Beta 5的机会,但还是没有运气.
我注意到,这种现象似乎是由包装在 ScrollView 组件中引起的.如果我删除ScrollView组件,一切都会按预期进行.
这是代码:
struct HomeList:查看{var listViewItems = listViewItemsData@State var show = falsevar body:some View {VStack {HStack {VStack(alignment:.lead){文本(项目标题").font(.largeTitle).fontWeight(.heavy)文本(项目字幕").foregroundColor(Color.gray)}垫片()} .padding(.top,78).padding(.leading,60)ScrollView(.horizontal,showsIndicators:false){HStack(间距:30){ForEach(listViewItems){GeometryReader {按钮(操作:{self.show.toggle()}){ListView(标题:item.title,图像:item.image,颜色:item.color,目的地:item.destination).rotation3DEffect(角度(度数:Double((geometry.frame(in:.global).minX-30)/-30)),轴:(x:0,y:10,z:0)).sheet(isPresented:self.$ show,content:{InformationView()})}} .frame(宽度:246,高度:360)}} .padding(30)垫片()} .frame(宽度:UIScreen.main.bounds.width,高度:480)垫片()}}}
总而言之,如果没有 ScrollView 包装器,模态行为将按预期工作.
我想知道是否有解决方案/解决方法?或者我只需要等待发布:)
根据答案进行编辑:
struct HomeList:查看{var listViewItems = listViewItemsData@State var show = false@State变量视图:AnyView = AnyView(Text("))var body:some View {VStack {HStack {VStack(alignment:.lead){文本(项目标题").font(.largeTitle).fontWeight(.heavy)文本(项目字幕").foregroundColor(Color.gray)}垫片()} .padding(.top,78).padding(.leading,60)ScrollView(.horizontal,showsIndicators:false){HStack(间距:30){ForEach(listViewItems){GeometryReader {按钮(操作:{self.show.toggle()self.view = item.destination}){ListView(标题:item.title,图像:item.image,颜色:item.color,目的地:item.destination).rotation3DEffect(角度(度数:Double((geometry.frame(in:.global).minX-30)/-30)),轴:(x:0,y:10,z:0))}} .frame(宽度:246,高度:360)}} .padding(30)垫片()} .frame(宽度:UIScreen.main.bounds.width,高度:480).sheet(isPresented:self.$ show,content:{self.view})垫片()}}}
这是与
I just upgrade to Xcode 11 Beta 5 and update my SwiftUI project.
In previous version I wanted to use PresentationLink component to show up a modal. I had the same problem than now, the modal has only shown once. I thought it was a bug as I saw in other SO posts. So I tried my chance by upgrading to Beta 5 but still no luck.
I noticed that this behaviour seems to be caused by wrapping in a ScrollView component. If I delete the ScrollView component everything works fine as expected.
Here's the code:
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()
}
}
}
To summarize, without ScrollView wrapper the Modal behaviour works as expected.
I would like to know if there is a solution / workaround ? Or I just have to wait a release :)
Edit from answer:
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()
}
}
}
This is the same issue as https://stackoverflow.com/a/57087399/3179416
Just move your .sheet
outside of your 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()
}
}
}
这篇关于Xcode 11 Beta 5-模式仅触发一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!