Вот код, который работал на swift 5.1
и взломал swift 5.2
. Я пытаюсь установить значение ключа предпочтения для использования рамки кнопки в другом месте:
struct SubmenuButtonPreferenceKey: PreferenceKey {
typealias Value = [CGSize]
static var defaultValue: Value = []
static func reduce(value: inout Value, nextValue: () -> Value) {
value.append(contentsOf: nextValue())
}
}
struct SubmenuButtonPreferenceViewSetter: View {
var body: some View {
GeometryReader { geometry in
Rectangle()
.fill(Color.clear)
.preference(key: SubmenuButtonPreferenceKey.self,
value: [geometry.frame(in: .local).size]) // crash here
}
}
}
public struct FloatingButton: View {
@State private var size = CGSize()
public var body: some View {
ZStack {
Button(action: {}) {
Text("a")
}
.background(SubmenuButtonPreferenceViewSetter())
}
.onPreferenceChange(SubmenuButtonPreferenceKey.self) { rect in
if let r = rect.first {
self.size = r
}
}
}
}
struct ContentView : View {
var body: some View {
return NavigationView {
VStack {
FloatingButton()
}
}
}
}
Сбой при
ошибка предварительного условия: неверный индекс ввода: 2
Если я запишу это в консоль, она покажет много ошибок, но затем покажет правильный кадр
po geometry.frame(in: .local)
=== AttributeGraph: cycle detected through attribute 1144 ===
=== AttributeGraph: cycle detected through attribute 995 ===
...more of these errors...
=== AttributeGraph: cycle detected through attribute 1007 ===
▿ (0.0, 0.0, 160.0, 45.0)
▿ origin : (0.0, 0.0)
- x : 0.0
- y : 0.0
▿ size : (160.0, 45.0)
- width : 160.0
- height : 45.0
Я попытался поиграть с тем, куда я помещаю GeometryReader и установщик предпочтений вызовов, но он всегда вылетает, если я Удалите настройщик предпочтений вообще.
ОБНОВЛЕНИЕ: обновлен код, теперь он действительно вылетает, извините за это. Он перестает падать, если я удаляю VStack или NavigationView, но они мне нужны точно в этих местах.