xcode 为什么iOS 15之后的状态栏会因为模糊效果而崩溃?

kcrjzv8t  于 2023-03-24  发布在  iOS
关注(0)|答案(2)|浏览(123)

我遇到了一个奇怪的问题,因为iOS 15:我在App's Root View上有一个Blur Effect,它会根据scenePhase而变化。在iOS 15发布之前,这是完美的。现在,每当Blur Effect为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开始,出现了以下错误:

  • 奇怪的是,当scenePhase变成inactive时,Navigation Bar立即跳到它的正确位置。而当scenePhase再次变成active时,它又跳回到Status Bar后面的顶部。
  • 此外,当将activescenePhaseBlur Radius更改为0.001而不是0时,一切都正常工作,Navigation Bar不会跳到Status Bar后面。

有没有人知道在使用Blur Effects时是什么原因导致了这种奇怪的行为?
非常感谢您的帮助提前。

k5ifujac

k5ifujac1#

我遇到了这个确切的问题,并且无法找到修复方法,所以我现在使用这个替代实现,它做了几乎相同的事情:

ZStack {
    // View to be blurred goes here
    Rectangle()
        .ignoresSafeArea()
        .foregroundStyle(.ultraThinMaterial)
        .opacity(/* Your condition */ ? 1 : 0)
        .animation(.easeInOut(duration: 0.2))
}

这会在你的视图上覆盖一个模糊的矩形。所以在你的例子中:

struct RootView: View {
    @Environment(\.scenePhase) var scenePhase

    var body: some View {
        ZStack {
            OtherViews()
            Rectangle()
                .ignoresSafeArea()
                .foregroundStyle(.ultraThinMaterial)
                .opacity(scenePhase != .active ? 1 : 0)
                .animation(.easeInOut)
        }
    }
}

由于此解决方案使用了新材质,因此仅适用于iOS 15。您可以使用if #available(iOS 15, *)提供两种不同的实现,一种适用于iOS 15+,另一种适用于iOS 14及更早版本。

p5fdfcr1

p5fdfcr12#

虽然我不确定这个解决方案是否明显优于其他解决方案,但由于它的简单性,我建议考虑使用它。

上一页

.blur(someCondition ? 2.0 : 0.0)

Hacky“fix”

.blur(someCondition ? 2.0 : 0.0001)

相关问题