【发布时间】:2020-12-11 16:56:06
【问题描述】:
每当我的代码变得太大时,SwiftUI 就会开始表现得很奇怪并产生错误:
"The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions"
所以我开始将我的代码分解为Extracted Subviews,我遇到的问题之一是如何从减去的子视图中消除视图。
例子:我们这里有LoginContentView这个视图包含一个按钮,当点击按钮时它会显示下一个视图UsersOnlineView。
struct LoginContentView: View {
@State var showUsersOnlineView = false
var body: some View {
Button(action: {
self.showUsersOnlineView = true
}) {
Text("Show the next view")
}
.fullScreenCover(isPresented: $showUsersOnlineView, content: {
UsersOnlineView()
})
}
另一方面,我们有一个提取到子视图的按钮,用于关闭模式并返回原始视图:
import SwiftUI
struct UsersOnlineView: View {
var body: some View {
ZStack {
VStack {
CloseViewButton()
}
}
}
}
struct CloseViewButton: View {
var body: some View {
Button(action: {
// Close the Modal
}) {
Text("Close the view")
}
}
}
【问题讨论】: