自从iOS 15发布以来,我遇到了一个奇怪的问题:我的应用程序根视图上有一个模糊效果(Blur Effect),这取决于场景(phase)。这一切都很完美,直到iOS 15发布为止。现在,每当模糊效果是0时,应用程序的状态栏(Status Bar)就会折叠起来,导航栏(Navigation Bar)会向上移动,并且无法再与之交互。
struct RootView: View {
@Environment(\.scenePhase) var scenePhase
@State private var blurRadius: CGFloat = 0
var body: some View {
Group {
OtherViews()
}
.blur(radius: blurRadius)
.onChange(of: scenePhase) { newValue in updateBlurRadius(newValue) }
}
private func updateBlurRadius(_ scenePhase: ScenePhase) {
switch scenePhase {
case .active : withAnimation { blurRadius = 0 }
case .inactive: withAnimation { blurRadius = 16 }
case .background: withAnimation { blurRadius = 16 }
@unknown default: print("Unknown Case")
}
}
}
这段代码在iOS 14及之前版本上运行良好,但是自从iOS 15以后出现了以下的bug:
- 奇怪的是,当
scenePhase
变成inactive
时,导航栏
瞬间跳到了正确的位置。而当scenePhase
再次变为active
时,它又跳回到状态栏
后面的顶部。 - 此外,当将
active
scenePhase
的Blur Radius
更改为0.001而不是0时,一切都正常工作,导航栏
不会跳到状态栏
后面。
有人知道这种使用模糊效果
时可能引起这种奇怪行为的原因吗?
提前感谢您的帮助。
ZStack
替换了Group
,但这并没有解决问题。此外,在 Group 对象内部是一个if-else
语句,而不仅仅是一个单独的视图。 - christophriepe