Если у вас есть swiftui contentView () , который отображает pausableView () на основе свойства @State PresestSheet , что Также используется для представления. sheet (), тело оценивается по-разному в зависимости от того, как в теле контентвийт используется свойство -Верд ">
Код: Выделить всё
struct ContentView: View {
@State private var presentSheet = false
var body: some View {
return VStack{
Button("Show Sheet") {
presentSheet.toggle()
}
PausableView(isPaused: presentSheet) // 1. passing the property as a normal variable
// evaluates the body and the sheet on dismiss
// PausableView(isPaused: $presentSheet) // 2. passing the property as a @Binding
// doesn't evaluate the body when it changes on dismiss
}
.sheet(isPresented: $presentSheet) {
DismissingView(isPresented: $presentSheet)
}
}
}
< /code>
[list]
[*] Если свойство отправлено в PausableView(isPaused: presentSheet)
[*] Если имущество отправлено в
Код: Выделить всё
PausableView(isPaused: $presentSheet)
[/list]
это нормальное поведение?
Кроме того, использование @Binding вместо этого, кажется, полностью изменит, как оценивается тело. Но отправка его в виде @Binding неверна, потому что свойство следует считывать только в представлении ребенка. Оценивается при использовании нормального свойства (см. Строки 27-28 и 53-54):
2-тело не оценивается при использовании @Binding (см. Строки 27-28 и 53-54): < br/>
Образец проекта
Пример проекта, созданный в Xcode 13, доступен здесь: https://github.com/clns/swiftui-sheet-redraw-on-dismiss. Я заметил то же поведение на iOS 14 и iOS 15. переопределить ">
Код: Выделить всё
import SwiftUI
struct DismissingView: View {
@Binding var isPresented: Bool
var body: some View {
if #available(iOS 15.0, *) {
print(Self._printChanges())
} else {
print("DismissingView: body draw")
}
return VStack {
Button("Dismiss") { isPresented.toggle() }
Text("Dismissing Sheet").padding()
}.background(Color.white)
}
}
struct PausableView: View {
var isPaused: Bool
// @Binding var isPaused: Bool
private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
@State private var counter = 0
var body: some View {
Text("Elapsed seconds: \(counter)")
.onReceive(timer) { _ in
counter += isPaused ? 0 : 1
}
}
}
struct ContentView: View {
@State private var presentSheet = false
var body: some View {
if #available(iOS 15.0, *) {
print(Self._printChanges())
} else {
print("ContentView: body draw")
}
return VStack{
Button("Show Sheet") { presentSheet.toggle() }
Text("The ContentView's body along with the .sheet() is being redrawn immediately after dismiss, if the @State property `presentSheet` is used anywhere else in the view - e.g. passed to `PausableView(isPaused:presentSheet)`.\n\nBut if the property is passed as a @Binding to `PausableView(isPaused:$presentSheet)`, the ContentView's body is not redrawn.").padding()
PausableView(isPaused: presentSheet)
// PausableView(isPaused: $presentSheet)
}
.sheet(isPresented: $presentSheet) {
DismissingView(isPresented: $presentSheet)
.background(BackgroundClearView()) // to see what's happening under the sheet
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/694 ... presenting